0

Not sure of the best way to do this.

I have some code to automatically scroll, like this:

$("html, body").animate({
    scrollTop: $('html, body').get(0).scrollHeight
}, (60000));

And I thought I could "stop" this scrolling by adding this:

$(window).on('scroll', function() {
    $("html, body").stop();
});

But it looks like that second bit of code is firing when the first bit of code executes. So I'm not achieving anything.

I just need a way to have the auto-scroll stop when the user wants to manually scroll or move the scroll bar.

All ideas welcome. Thanks.

b85411
  • 9,420
  • 15
  • 65
  • 119

1 Answers1

2
$('body,html').bind('scroll mousedown wheel DOMMouseScroll mousewheel keyup', function(e){
 if ( e.which > 0 || e.type == "mousedown" || e.type == "mousewheel"){
  $("html,body").stop();
 }
});

I found your answer at

How can I differentiate a manual scroll (via mousewheel/scrollbar) from a Javascript/jQuery scroll?

I've added jsbin to describe

http://jsbin.com/dujuxihota/1/edit?js,output

Community
  • 1
  • 1
Amit Choukroun
  • 505
  • 5
  • 14
  • I have another question. I am using this code: `$("html, body").animate({ scrollTop: $('html, body').get(0).scrollHeight }, 6000);` but after 6 seconds it still hasn't reached the bottom. I have a setInterval to change the page after 6 seconds, so it's very important that the scroll reaches the bottom before the 6 seconds is up. Is there something wrong with the code I'm using? – b85411 Nov 12 '14 at 06:21