Reputation: 1168
Currently on my site (with Bootstrap) when a user clicks on an anchor with a link to the same page, I use the following jquery to make it scroll to the link, rather than jump:
jQuery('html,body').animate({scrollTop: jQuery('#target').offset().top}, 1000);
It works, but, I've seen some sites that use an animation that starts off slower and then speeds up as it scrolls and almost has a little bounce at the end. It's very smooth/slick. I'm looking to achieve something similar.
Anyone know of other ways to animate scrolling via links? I'd prefer to avoid jquery plugins if possible.
Thanks
Upvotes: 0
Views: 431
Reputation: 8403
The default easing of jQuery is swing
and the only vanilla option is linear
.
While you said you'd prefer not to use plugins, easy option would be including jQueryUI easings into the project. It offers tons of options, look into the easings page and choose the one you prefer if swing
doesn't satisfy (or make your own!). jQueryUI's Effects core weights in at 13kb's.
Upvotes: 1
Reputation: 443
Add easing to the animate method
jQuery('html,body').animate({scrollTop: jQuery('#target').offset().top}, {duration: 1000, easing: 'easeOutBounce' });
*untested
Docs here: http://api.jquery.com/animate/
Examples here: http://www.learningjquery.com/2009/02/quick-tip-add-easing-to-your-animations/
Upvotes: 1