How can I differentiate a manual scroll (via mousewheel/scrollbar) from a Javascript/jQuery scroll?
Posted
by David Murdoch
on Stack Overflow
See other posts from Stack Overflow
or by David Murdoch
Published on 2010-05-14T13:59:20Z
Indexed on
2010/05/14
17:14 UTC
Read the original article
Hit count: 265
UPDATE:
Here is a jsbin example demonstrating the problem.
Basically, I have the following javascript which scrolls the window to an anchor on the page:
// get anchors with href's that start with "#"
$("a[href^=#]").live("click", function(){
var target = $($(this).attr("href"));
// if the target exists: scroll to it...
if(target[0]){
// If the page isn't long enough to scroll to the target's position
// we want to scroll as much as we can. This part prevents a sudden
// stop when window.scrollTop reaches its maximum.
var y = Math.min(target.offset().top, $(document).height() - $(window).height());
// also, don't try to scroll to a negative value...
y=Math.max(y,0);
// OK, you can scroll now...
$("html,body").stop().animate({ "scrollTop": y }, 1000);
}
return false;
});
It works perfectly......until I manually try to scroll the window. When the scrollbar or mousewheel is scrolled I need to stop the current scroll animation...but I'm not sure how to do this.
This is probably my starting point...
$(window).scroll(e){
if(IsManuallyScrolled(e)){
$("html,body").stop();
}
}
...but I'm not sure how to code the IsManuallyScrolled
function. I've checked out e
(the event
object) in Google Chrome's console and AFAIK there is not way to differentiate between a manual scroll and jQuery's animate()
scroll.
How can I differentiate between a manual scroll and one called via jQuery's $.fn.animate
function?
© Stack Overflow or respective owner