I am trying to learn jQuery and I’m having a mental blank at the moment. The following code scrolls my page to the top with a smooth transition, but I would like this smooth transition to work for all anchor/ID links on my site (it’s only a one pager).
$(document).ready(function() {
$('a[href="#the-top"]').click(function (e) {
$("html, body").animate({ scrollTop: $('#the-top').offset().top }, 1000);
return false;
});
});
How can I change my code to achieve this?
Changes suggested in this code:
$object tojQueryjQuery(fn)asdocument.ready(fn)jQueryas$inside that functionreturn false(source: http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/)$whatasking for the#somethingpart of anchor href, in order to prevent misbehaviors in IE (because if you havehref="#some"sometimes it becomehref="http://yoursite.com/yourcurrentpage/#someinstead)All of these are kind of optional. You get the idea. Feel free to change.
DEMO AT: http://jsfiddle.net/Nm3cT/