Created
July 28, 2011 10:35
-
-
Save sunny/1111349 to your computer and use it in GitHub Desktop.
JavaScript that scrolls down on page load to `<h3>Important</h3>` if you go to `foo.html#Important`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Scroll on page load to the first header where the text matches the hash in the URL | |
// For example going to 'foo.html#Important' will scroll down to '<h3>Important</h3>' | |
$(function() { | |
var h = window.location.hash.replace('#', '') | |
if ($(h).length) | |
$('h2,h3,h4,h5,h6').each(function() { | |
if ($.trim($(this).text().replace(/\s+/g, ' ')) == h) | |
return window.scrollTo(this.offsetLeft, this.offsetTop) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment