«

»

Mar 20

Replace default hashtag scrolling in browser

Tired of the fluid-less jumps when going to a URL with a #hashtag? You can replace this using jQuery’s .scrollTo() method in the document.ready of your javascript. Here’s an example:

$(document).ready(function() {
	if(window.location.hash)
	{
		$('html, body').animate({
			scrollTop: $(window.location.hash).offset().top
		}, 1000);
	}
});

Now on page load this will fluidly scroll to the DOM element you’ve requested. I have not added error handling if say, the hashtag does not exist in the DOM, but you get the general idea.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>