繁体   English   中英

我如何将图像放置在我的画廊的中心

[英]how to i get the image to center in my gallery

我为我的站点制作了一个简单的滑块库,但是发现当我单击下一步时,图像更新,但是直到完成整个图像周期后,它才会居中

我如何才能从一开始就对齐图像?

这是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;}

JQUERY

    //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();
    });

您遇到的问题是,单击prev / next后立即调用“更新”功能。 该图像尚未加载,因此代码实际上还不知道新的gal.width。 这就是为什么它在一整轮之后都可以工作的原因:图像现在在缓存中,因此已经可用。

最好的解决方案是使用javascript的Image对象预加载图片。 一种更简单但可能有问题的方法是使用“加载”事件(它可能无法在所有浏览器中正常运行)。

您可以将库div与一些简单的CSS hack对齐。

1)首先定义宽度。 (您可以使用jquery定义动态宽度)。

2)添加位置:绝对;

3)添加left:0,right:0;

4)增加保证金:0自动;

最终代码如下所示。

.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;
}

您的数学错误,请看下面的示例http://jsfiddle.net/8pScd/6/

我只需要更改您的数学

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

我在您的CSS 左侧删除了这行:50%;

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM