0

我已经使用这个脚本在我的网站上创建一个无限轮播。它是用 CSS 定制的,所以第一个和最后一个项目显示一半。

如果你继续点击右箭头,你最终会在最后碰到一个空白区域。到目前为止,我还无法解决此问题。任何人都可以提供任何解决方案吗?

这是相关的脚本:

/**
 * @author Stéphane Roucheray 
 * @extends jquery
 */
jQuery.fn.simplecarousel = function(previous, next, options){
    var sliderList = jQuery(this).children()[0];

    if (sliderList) {
        var increment = jQuery(sliderList).children().outerWidth(true),
        elmnts = jQuery(sliderList).children(),
        numElmts = elmnts.length,
        sizeFirstElmnt = increment,
        shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
        firstElementOnViewPort = 1,
        isAnimating = false;

        for (i = 0; i < shownInViewport; i++) {
            jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + increment + "px");
            jQuery(sliderList).append(jQuery(elmnts[i]).clone());
        }

        jQuery(previous).click(function(event){
            if (!isAnimating) {
                if (firstElementOnViewPort == 1) {
                    jQuery(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
                    firstElementOnViewPort = numElmts;
                }
                else {
                    firstElementOnViewPort--;
                }

                jQuery(sliderList).animate({
                    left: "+=" + increment,
                    y: 0,
                    queue: true
                }, "swing", function(){isAnimating = false;});
                isAnimating = true;
            }

        });

        jQuery(next).click(function(event){
            if (!isAnimating) {
                if (firstElementOnViewPort > numElmts) {
                    firstElementOnViewPort = 2;
                    jQuery(sliderList).css('left', "0px");
                }
                else {
                    firstElementOnViewPort++;
                }
                jQuery(sliderList).animate({
                    left: "-=" + increment,
                    y: 0,
                    queue: true
                }, "swing", function(){isAnimating = false;});
                isAnimating = true;
            }
        });
    }
};

#home-carousel-container {
    position: relative;
}
#home-carousel {
    overflow: hidden;
}
#home-carousel ul {
    margin-left: -143px;
    padding: 0;
    position: relative;
}
#home-carousel li {
    float: left;
    height: 645px;
    list-style: none outside none;
    margin: 0 3px;
    width: 256px;
}

在此处输入图像描述

4

1 回答 1

2

根据我的评论。

您在轮播上设置了负的左边距,导致它隐藏了一半的图像。因此,当您单击下一个/上一个时,它会显示图像移动到的位置以创建连续效果。

在你的 CSS 中,我改变了

#home-carousel ul{
  position: relative;
  padding: 0;
  margin-left: -143px;
}

#home-carousel ul{
  position: relative;
  padding: 0;
  margin-left: -3px;
}

并且没有任何问题。

修改后的页面截图 - 在 Chrome 中

于 2013-03-04T15:44:40.433 回答