0

我有一组图像,所有图像的高度相同但宽度不同。我需要保持固定宽度,并且我需要图像无限循环地向左滑动。我还需要图像来填充容器宽度,以便显示部分图像。滚动将分步进行(例如 200 像素)并以设定的时间间隔进行。

我浏览了很多 jQuery 幻灯片插件,包括 Cycle、NivoSlider、EasySlider 和其他六个。但我所见过的似乎都没有让我确切地知道客户需要它做什么。我不需要可点击的导航或居中。只是一个连续的幻灯片,以设定的宽度步长和设定的间隔滚动。

我在想,就连续循环部分而言,我可以等到最左边的图像消失。最初,我尝试只使用append(),但是当最左边的移动到最后时,幻灯片会跳动。于是我就试着把它append()一个clone()到最后$('#slides').children(),然后remove()从头开始$('#slides')

到目前为止,我有:

<div id="outer" style="width:300px;">
    <div id="slides">
        <img src="/images/slide1.jpg" alt="" style="height:150px;width:200px;" />
        <img src="/images/slide2.jpg" alt="" style="height:200px;width:200px;" />
        <img src="/images/slide3.jpg" alt="" style="height:150px;width:200px;" />
        <img src="/images/slide4.jpg" alt="" style="height:300px;width:200px;" />
    </div>
</div>

/* some code ideas taken from easySlider */
(function($){
    $.fn.stepSlider = function() {
        var slideInterval;

        this.each(function() {
            // this will be the width of $('#slides')
            var width = 0;
            // shortcut I saw in easySlider code; liked it, used it.
            var o = $(this);
            // set width of $('#slides') based on total width of child images
            o.children().each(function(){width+=$(this).width();});

            function scroll() {
                o.children().each(
                    function(index){
                        // slide each of the children to the left by 150px
                        $(this).animate({'left':'-=150'}, 2000);
                    }
                );

                // after children have been animated() left, check to see
                // if the left-most child has gone out of sight
                if (o.children(':first-child').position().left + o.children(':first-child').width() < 0) {
                    // cloning the first child now
                    var el = o.children(':first-child').clone(true);
                    // removing the first child
                    o .children(':first-child').remove();
                    // trying to reset the css('left') of all of the children now that the first
                    // element is supposed to be gone
                    o.children().css('left', o.children(':first-child').position().left + o.children(':first-child').width());
                    // appending the clone()
                    o.append(el);
                }
            }

            slideInterval = setInterval(function(){scroll();}, 2000);
        });
    }
})(jQuery);

滑动工作。但是将第一张图像移到最后会导致幻灯片跳动。然后事情就开始崩溃到完全停止的地步。

非常感谢任何见解。不,我对 jQuery 并不完全陌生,但是是的:这是我第一次尝试插件。

谢谢和最好的问候

4

2 回答 2

1

我建议您可以使用 scrollTo 插件(或自己编写)。以下内容会将一个区域滚动 300 像素,因此无论您在其中放置什么都没关系。您必须管理该区域的位置和宽度,以便在最后停止或根据需要进行操作。

滚动到: http ://demos.flesler.com/jquery/scrollTo/

JSFiddle:http: //jsfiddle.net/YZ9vA/2/

    $(document).ready(function(){
    var areaWidth = 0, x=0;        
    $('#slideshow img').each(function() {
        areaWidth = areaWidth + $(this).width();
    });
    $('#slideshow').width(areaWidth);

    function ScrollMe() {
     x += 300;
     $('#sliders').scrollTo({top:0, left:x},800);

     }


   setInterval(ScrollMe,1000);

});​

原答案:

我对您的建议是使用将永远重复的 jquery 循环。不是循环图像,而是循环 div,您可以将这些宽度设置为 200 像素,并且可以根据需要放置内部的图像(居中,宽度 100% 等)。

http://jquery.malsup.com/cycle/

#slides, #slides div {width:200px;height:300px}
#slides img {width:100%;height:100%}  

<div id="slides">
    <div><img src="/images/slide1.jpg" alt="" style="" /></div>
    <div><img src="/images/slide2.jpg" alt="" style="" /></div>
    <div><img src="/images/slide3.jpg" alt="" style="" /></div>
    <div><img src="/images/slide4.jpg" alt="" style="height:300px;width:200px;" /></div>
<!-- I left the last inline style on, you can use that to override or use css externally -->
</div>

$('#slides').cycle({fx:'scrollHorz', 
                   continuous: 1, 
                   easeIn: 'linear',
                   easeOut: 'linear'});
于 2012-05-15T21:34:57.080 回答
0

所以感谢所有 lucuma 的帮助,我有一些工作。

CSS:

<style type="text/css">
    #outer  {height: 250px; width: 760px; overflow:hidden;}
    #mid    {height: 250px; width: 760px; overflow:hidden;}
    #slides {height: 250px; width: auto; overflow:hidden;}
    #slides img {float: left; height: 250px;} /* height specified just for clarity */
</style>

HTML:

我不得不将#slides div 包装在另外两个 div 中,而不是一个。只有一个包装器,scrollTo 插件最终将 div 设置为 100% 宽度,而不是遵守我的 760px 约束。由于时间关系,我没有进一步研究这个。我刚刚添加了额外的 div 并称之为一天。

<div id="outer">
    <div id="mid">
        <div id="slides">
            <img src="images/image1.jpg" alt="" style="width:760px;" />
            <img src="images/image2.jpg" alt="" style="width:529px;" />
            <img src="images/image3.jpg" alt="" style="width:720px;" />
            <img src="images/iamge4.jpg" alt="" style="width:563px;" />
            <!-- my live version has 16 total images -->
        </div>
    </div>
</div>

JavaScript:

<script type="text/javascript" src="scripts/jquery.scrollTo.min.js"></script>
<script type="text/javascript">
    $(function(){
        var areaWidth = 0, x = 0, lelements=$('#slides img').length * -1;

        /* This is the same essential functionality that I had in my original post
         * to set the width of the images container
         */
        function setWidth() {
            $('#slides img').each(function() {
                areaWidth = areaWidth + $(this).width();
            });
            $('#slides').width(areaWidth);
        }

        /* This is also essentially what I was doing before: checking
         * to see if an image is no longer viewable. It appends a clone
         * to the $('#slides'), but it differs from my original idea by
         * not trying to remove any :first element.
         * So $('#slides img').length will continue to increase as the
         * program runs. I was trying to avoid this, but in reality, it
         * probably doesn't matter a ton unless someone is watching the
         * same page for quite a long time.
         */
        function checkIt() {
            $('#slides img').slice(lelements).each(function() {
                if ($(this).position().left + $(this).width() < 0) {
                    $(this).clone().appendTo($('#slides'));
                }
            });
        }            

        /* This is where we use Flesler's scrollTo plugin to handle the motion.
         * I changed scrollTo({}, 3000) to scrollTo({}, {duration: 1000})
         * to slow the transition speed.
         */
        function scrollMe() {
            $('#outer').scrollTo({top:0, left:x}, {duration:1000});

            /* x += 300 was the first line in scrollMe(), but ended up
             * resulting in a 600px slide for the first transition,
             * so I moved it here. The first transition takes longer
             * to occur, but all other transitions are fine
             */
            x += 300;

            checkIt();
            setWidth();
        }

        setInterval(scrollMe, 3000);  
    });
</script>

我必须做的另一件事是确保我的第一张幻灯片是全宽(760 像素)。如果小于这个值,那么第二个图像最初是不可见的,然后它会突然出现在容器的左侧空间中,然后会发生滚动。随后的所有图像都很好。显然,让第一张图像填满整个宽度会消除该伪影。

再次感谢 lucuma - 你帮了很多忙!

于 2012-05-16T16:43:31.730 回答