简体   繁体   English

重复水平自动滚动条

[英]Repeating horizontal auto scroll bar

i have an auto scrolling bar script that makes the images scroll right. 我有一个自动滚动条脚本,可以使图像向右滚动。 I want to make it repeat after reaching the end. 我想在结束后再说一次。 Please help! 请帮忙!

<div class="scrolls">
    <div>
        <img src="img1" height="200"/>
        <img src="img2" height="200"/>
        <img src="img3" height="200"/>
    </div>          
</div>

<script>
    $(document).ready(function() {
        var sL = 4000;
        $('.scrolls').animate({
            scrollLeft : sL
        },100000, 'linear');

        $(".scrolls").on("click",function(){
                $(this).stop(true,false);
        });

        $(".scrolls").on("mouseenter",function(){
            $(this).stop(true,false);
        });

        $(".scrolls").on("mouseleave",function(){
            $(this).animate({
            scrollLeft : sL
            },100000, 'linear');
        });
    })
    </script>

Take an unordered list with listitems (html markup) 使用listitems(html标记)获取无序列表

<div class="scrolls">
    <ul>
        <li><img src="img1" height="200"/></li>
        <li><img src="img2" height="200"/></li>
        <li><img src="img3" height="200"/></li>
    </ul>
</div>

Copy this listitems so you can fillup the whole horizontal space twice. 复制此列表项,以便您可以两次填充整个水平空间。 Now scroll the div to the right and everytime an image leaves the div to the left, move it from the beginning to the end. 现在,将div滚动到右侧,每当图像离开div到左侧时,将其从头到尾移动。

// create as many copys as needed for filling the whole harizontal space twice
var dummyClones = $('.scrolls > ul > li').clone(true);
while($('.scrolls > ul').width() < 2*$('.scrolls').width())
  $('.scrolls > ul').append(dummyClones.clone(true));

The animation itself is solved through window.requestAnimationFrame : 动画本身是通过window.requestAnimationFrame解决的:

var animationSpeedInMiliseconds = 10000; // 10 seconds for 1000px
function step(timestamp) {
    if(!step.startTimestamp) step.startTimestamp = timestamp;
    if(!step.stopped) {
        let delta = timestamp - step.startTimestamp;
        let pixelCountToShift = (delta / animationSpeedInMiliseconds) * 1000;
        if($('.scrolls').scrollLeft()+pixelCountToShift > $('.scrolls > ul > li:eq(0)').width()) {
            $('.scrolls > ul').append($('.scrolls > ul > li:eq(0)'));
            $('.scrolls').scrollLeft($('.scrolls').scrollLeft()+pixelCountToShift-$('.scrolls > ul > li:eq(0)').width());
        } else {
            $('.scrolls').scrollLeft($('.scrolls').scrollLeft()+pixelCountToShift);
        }
    }
    step.startTimestamp = timestamp;
    window.requestAnimationFrame(step);
}

Attention: It's not perfect. 注意:这并不完美。 It's an example I created in 4 minutes and it's meant to be understood as an proof-of-concept. 这是我在4分钟内创建的一个示例,它被理解为概念证明。 Not a script for productional use. 不是用于生产的脚本。

jsfiddle: https://jsfiddle.net/Kasalop/tktfr4rz/2/ jsfiddle: https ://jsfiddle.net/Kasalop/tktfr4rz/2/

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

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