I have set up a snippet that scrolls a page section into view when it's clicked, the problem is that if the user wants to scroll in the middle of the animation the scroll kinda stutters.
$( "section" ).click(function(e) {
$('html,body').animate({ scrollTop: $(this).position().top }, 'slow');
return false;
});
How can I stop the jquery animation if the user scrolls manually?
Best Answer
Change your function to this:
This will:
Some extra information:
There are many different types of scroll events. You can scroll down using your mouse, keyboard, touchscreen, etc. With this we make sure to catch all of them.
If you use the same selector more than once, it's good practice to cache them in a variable. It will be easier to write/use, and has better performance than having the program re-calculate the selector every time.
You can read the jQuery documentation for .animate() and .stop(). I also recommend reading about animation queue's since
.stop()
works on this principle.