admin管理员组文章数量:1335138
I have a scrollable div that I want to scroll down 50 pixels every X seconds. That's fine and working.
I also have a seperate function that scrolls the div back to the top when it reaches the bottom. Also fine; working.
Now, I need to bine the two so the scrolldown is ignored until we have scrolled to the top again.
I have a 'working' example here, as you'll see it has some pretty nutty behavior: /
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);
}
});
I have a scrollable div that I want to scroll down 50 pixels every X seconds. That's fine and working.
I also have a seperate function that scrolls the div back to the top when it reaches the bottom. Also fine; working.
Now, I need to bine the two so the scrolldown is ignored until we have scrolled to the top again.
I have a 'working' example here, as you'll see it has some pretty nutty behavior: http://jsfiddle/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);
}
});
Share
Improve this question
edited Oct 25, 2012 at 11:03
VisioN
145k34 gold badges287 silver badges289 bronze badges
asked Oct 25, 2012 at 11:03
Alan ShortisAlan Shortis
1,1093 gold badges18 silver badges38 bronze badges
2 Answers
Reset to default 4My version:
var scrollingUp = 0;
window.setInterval(scrollit, 3000);
function scrollit() {
if(scrollingUp == 0) {
$('#scroller').delay(2000).animate({ scrollTop: $("#scroller").scrollTop() + 50 }, 'slow');
}
}
$('#scroller').bind('scroll', function () {
$('#status').html(scrollingUp);
if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
scrollingUp = 1;
$('#scroller').delay(2000).animate({ scrollTop: 0 }, 1000, function() {
scrollingUp = 0;
});
}
});
Demo: http://jsfiddle/EFmeK/
Btw, in your jsfiddle, it scrolls 60px instead of 50px, which I "fixed" in my example.
Try something like that : http://jsfiddle/JVftf/3/
window.setInterval(scrollit, 1000);
function scrollit() {
console.log(($("#scroller").scrollTop() + $("#scroller").innerHeight()))
console.log($("#scroller")[0].scrollHeight)
if(($("#scroller").scrollTop() + $("#scroller").innerHeight()) >= $("#scroller")[0].scrollHeight)
$('#scroller').animate({ scrollTop: 0 }, 100).delay(900);
else
$('#scroller').animate({ scrollTop: $("#scroller").scrollTop() + 60 }, 'slow',function(){
});
}
本文标签: javascriptjQueryscroll down every x secondsthen scroll to the topStack Overflow
版权声明:本文标题:javascript - jQuery - scroll down every x seconds, then scroll to the top - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742218676a2435037.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论