jquery - javascript scroll to top automatically unless user starts scrolling -


i automatically scroll top, bail out if user starts scrolling.

what have stops animation prematurely because scrolling animation scrolling - triggers "stop scrolling if scrolling happens" action.

function stop_scrolling_to_top(){     // stop animation attached selector     $('html, body').stop(); }  // scroll top automatically $('html, body').animate({ scrolltop: 0}, 1400, "easeoutquint", function(){      // callback when animation complete     do_not_do_when_scrolling(stop_scrolling_to_top); });  // stop animation if scrolling starts do_when_scrolling(stop_scrolling_to_top); 

is there way determine if scrolling triggered human or js? there better way entirely?

hopefully help.

it listens mousewheel event + keys can used scroll on page (page up/down, spacebar, arrowkeys etc):

$(document).keyup(function(e){      if($.inarray(e.which, [33,34,32,38,40]) !== -1)         console.log('key');         //potential scroll key  });  $(document).bind((/firefox/i.test(navigator.useragent))                                   ? 'dommousescroll'                                   : 'mousewheel', function(e){      var evt = window.event || e;        evt = evt.originalevent ? evt.originalevent : evt;                    var delta = evt.detail ? evt.detail*(-40) : evt.wheeldelta;      if(delta > 0)          console.log('mousewheel up');         //scroll     else         console.log('mousewheel down');         //scroll down   }); 

and if notice 'your' scroll active, stop did above:

if($('html, body').is(':animated'))     $('html, body').stop(); 

http://jsfiddle.net/mqq3f/1/


Comments