0

我为我的网站制作了一个简单的滑块库,但发现当我单击下一个图像更新时,它不会居中,直到我完成了完整的图像循环

我怎样才能让图像从一开始就对齐?

这是 JS 小提琴 > http://jsfiddle.net/8pScd/4

HTML

<div class="view_gallery">view gallery</div>

<div class="prev control"><<</div>
<div class="next control">>></div>

<div class="gallery">

</div>

<div class="overlay"></div> 

CSS

.overlay{
    display: none;
    position: absolute; top: 0; right: 0; bottom: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(0,0,0,0.8);
    z-index: 100;
}

.gallery{
    z-index: 200;
    padding: 10px;
    position: absolute;
    left: 50%; 
    background: #fff;
}

.control{
    position: absolute;
    top: 200px;
    z-index: 300;
    color: #fff;
    text-transform: capitalize;
    font-size: 2em;
    cursor: pointer;
}

.prev{left: 0;}
.next{right:0;}

查询

    //images
    var pics = new Array();
    pics[0] = "cars.jpg";
    pics[1] = "cats.png";
    pics[2] = "dogs.png";
    pics[3] = "bus.jpg"

    //total amount of pictures to display
    var pictot = pics.length-1;

    var nxt = $(".next"),
        prv = $(".prev"),
        view = $(".view_gallery"),
        gal = $(".gallery"),
        overlay = $(".overlay"),
        num = 0;

    //view gallery
    view.click(function(){
        overlay.show();
        gal.show();
        // Start gallery off on the first image
        gal.html('<img src="' + pics[0] + '" />');
    });

    nxt.click(function(){
        // If on the last image set value to 0. Else add 1
        if (num == pictot){num = 0;}else{num++;};
        update();
    });

    prv.click(function(){
        // If on first image set value to last image number. Else minus 1
        if (num == 0){num = pictot;}else{num--;}
        update();
    });

    function update () {
        // update image with next/previous
        gal.html('<img src="' + pics[num] + '" />');
        //center image (not working very well)
        var x = gal.width()/2;
        gal.css("marginLeft", -x);
    };

    //hide
    overlay.click(function(){
        gal.hide();
        $(this).hide();
    });
4

4 回答 4

1

您遇到的问题是单击上一个/下一个后立即调用“更新”功能。图像尚未加载,因此代码实际上还不知道新的 gal.width。这就是它在一个完整的回合后起作用的原因:图像现在在缓存中,因此已经可用。

最好的解决方案是使用 javascript Image 对象来预加载图片;一种更简单但可能存在问题的方法是使用“加载”事件(它可能不适用于所有浏览器)。

于 2013-11-14T15:11:43.097 回答
0

You can align your gallery div with some simple css hack.

1)first define width. (you can define dynamic width with jquery).

2)add position:absolute;

3)add left:0 , right:0;

4)add margin:0 auto;

final code looks like this.

.gallery {
    background: none repeat scroll 0 0 #FFFFFF;
    left: 0;
    margin: 0 auto !important;
    padding: 10px;
    position: absolute;
    right: 0;
    width: 600px;
    z-index: 200;
}
于 2013-11-14T14:57:14.617 回答
0

your math is wrong, look at this example http://jsfiddle.net/8pScd/6/

i've just need to change your math at

 var x = $('body').width()/2 - gal.width()/2;
 gal.css("margin-left", x + 'px');    

and i removed this line at your css left: 50%;

.gallery{
    z-index: 200;
    padding: 10px;
    position: absolute;
    background: #fff;
}
于 2013-11-14T14:57:46.153 回答
0

知道那.gallery是 920px 宽,设置left: 50%; margin-left: -470px. 还要删除 javascript 中更新画廊容器左侧边距的行 -gal.css("marginLeft", -x);

于 2013-11-14T15:00:15.697 回答