简体   繁体   English

jQuery无尽滚动循环

[英]jQuery Endless Scrolling loop

I have a project on jsFiddle: 我在jsFiddle上有一个项目:

jsFiddle Link jsFiddle链接

JavaScript: JavaScript的:

var scroller = function() {
    $('#holder div').animate({
        left: ($t.attr('id') == 'prev' ? '-=' : '+=') + 3
    }, 10, 'linear', function() {
        if ($(this).position().left < -$('li:first-child', this).width()) {
            w = $('li:first-child', this).totalWidth();
            $('li:first-child', this).appendTo('ul', this);
            $(this).css('left', $(this).position().left + w);
        }
    });
};

$.fn.extend({
    totalWidth: function() {
        return this.outerWidth() + parseInt(this.css('margin-left'), 10) + parseInt(this.css('margin-right'), 10);
    }
});
wdth = 0;
$('#marquee ul li').each(function() {
    wdth += $(this).totalWidth();
});
$('#holder div').width(wdth);
var to;
$('#prev, #next').css('top', ($('#marquee').outerHeight() - $('#prev').outerHeight()) / 2).live('mousedown mouseup', function(e) {
    $t = $(this);
    if (e.type == 'mousedown') {
        to = setInterval(scroller, 15);
    }
    else {
        clearInterval(to);
    }
});

HTML: HTML:

<div id="marquee">
    <div id="prev"><</div>
    <div id="next">></div>
    <div id="holder">
        <div>
        <ul>
            <li>Part 1</li>
            <li>Part 2</li>
            <li>Part 3</li>
            <li>Part 4</li>
            <li>Part 5</li>
            <li>Part 6</li>
            <li>Part 7</li>
            <li>Part 8</li>
            <li>Part 9</li>
            <li>Part 10</li>
        </ul>
    </div>
    </div>
</div>

CSS: CSS:

* {
    font-family: verdana;
    font-size: 12px;
}
#marquee {
    top: 50px;
    position: relative;
    width: 80%;
    height: 34px;
    background-color: #CCC;
    margin: 0 auto;
    padding: 0;
}
#holder {
    overflow: hidden;
position: absolute;
    left: 5px;
    right: 5px;
    top: 5px;
    bottom: 5px;
}
#holder div {
    position: absolute;
}
#marquee ul li {
    display: inline-block;
    float: left;
    margin-left: 5px;
    padding: 5px 7px;
    background-color: #555;
}
#marquee ul li:nth-child(2n+1) {
background-color: #888;
}
#marquee ul li:first-child {
    margin-left: 0;
}
#prev, #next {
    position: absolute;
    top: 10px;
    background-color: #66F;
    padding: 2px;
    cursor: pointer;
    z-index: 9002;
}
#prev {
    left: -13px;
    border-radius: 5px 0 0 5px;
}
#next {
    right : -13px;
    border-radius: 0 5px 5px 0;
}

what I am trying to achieve is a scrolling loop on mousedown, stopping on mouseup. 我想要实现的是在mousedown上滚动一个循环,在mouseup上停止。

I have been able to make it scroll, and loop but it 'jumps' on every loop change. 我已经能够使其滚动并循环播放,但是每次循环更改时它都会“跳转”。

Does anyone have any ideas? 有人有什么想法吗?


I have edited the fiddle to remove the CSS rule, also edited the code slightly. 我已经编辑了小提琴以删除CSS规则,还稍微编辑了代码。

Originally when it scrolled left, it jumped back about 20px and this was made to seem worse by the CSS rule! 最初,当它向左滚动时,它跳回了约20像素,而CSS规则使它看起来更糟!

Also the animation goes on in 10ms but it loops every 15ms due to the fact that on mouseup the loop would continue for a bit. 动画也会在10毫秒内继续播放,但由于每过15秒钟就会循环播放一次,因此动画每15毫秒播放一次。

The right does not loop because since I did not know how to do the left, I would not waste time making it scroll right erroneously when i could simply make it scroll correctly when it was ready. 右边不会循环,因为既然我不知道该怎么做,我不会浪费时间使它在正确准备好时可以正确地滚动,而错误地向右滚动。

I am not using a plugin because I want to learn how to do it myself (stubborn!) 我没有使用插件,因为我想自己学习(固执!)

Forked your fiddle and added code a) that eliminates jumpiness while scrolling and b) that checks and adds the first <li> to the end of the list (if there's space) while scrolling right (next) 分叉你的小提琴,并添加了代码a)消除滚动时的跳动,b)检查向右滚动(下一个)并将第一个<li>到列表的末尾(如果有空格)

Check this fiddle: http://jsfiddle.net/CRy4Q/15/ 检查此小提琴: http : //jsfiddle.net/CRy4Q/15/

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

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