jQuery - 每隔x秒向下滚动一次,然后滚动到顶部

Ala*_*tis 5 javascript jquery

我有一个可滚动的div,我想每X秒向下滚动50个像素.那很好,很有效.

我还有一个单独的功能,当它到达底部时将div滚动回顶部.也很好; 工作.

现在,我需要将两者结合起来,以便忽略scrollldown,直到我们再次滚动到顶部.

我在这里有一个"工作"的例子,因为你会看到它有一些非常疯狂的行为:http://jsfiddle.net/JVftf/

window.setInterval(scrollit, 3000);

function scrollit() {
    $('#scroller').delay(2000).animate({ scrollTop: $("#scroller").scrollTop() + 50 }, 'slow');
}

$('#scroller').bind('scroll', function () {
    if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
        $('#scroller').delay(2000).animate({ scrollTop: 0 }, 1000);
    }
});
Run Code Online (Sandbox Code Playgroud)

sas*_*cha 4

我的版本:

\n\n
var scrollingUp = 0;\n\nwindow.setInterval(scrollit, 3000);\n\nfunction scrollit() {\n    if(scrollingUp == 0) {\n        $(\'#scroller\').delay(2000).animate({ scrollTop: $("#scroller").scrollTop() + 50 }, \'slow\');\n    }\n}\n\n$(\'#scroller\').bind(\'scroll\', function () {\n    $(\'#status\').html(scrollingUp);\n\n    if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {\n        scrollingUp = 1;      \n        $(\'#scroller\').delay(2000).animate({ scrollTop: 0 }, 1000, function() {\n            scrollingUp = 0;    \n        });\n    }\n});\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n\n

演示: http: //jsfiddle.net/EFmeK/

\n\n

顺便说一句,在你的 jsfiddle 中,它滚动 60px 而不是 50px,我在示例中“修复”了它。

\n