Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  166] [ 1]  / answers: 1 / hits: 16910  / 14 Years ago, fri, may 14, 2010, 12:00:00

UPDATE:



Here is a jsbin example demonstrating the problem.



UPDATE 2:

And here is the fixed version thanks to fudgey.






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?


More From » jquery

 Answers
3

Try this function:



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


Also, did you see this tutorial?



Update: Modern browsers now use wheel as the event, so I've included it in the code above.


[#96786] Tuesday, May 11, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayaw

Total Points: 749
Total Questions: 88
Total Answers: 86

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
tayaw questions
;