Last active
February 11, 2024 20:37
-
-
Save dutchcelt/7563661 to your computer and use it in GitHub Desktop.
Hash location and stay put!
This file contains hidden or 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
var hashThis = function( $elem, cb ){ | |
var scrollLocation; | |
$( $elem ).on( "click", function( event ){ | |
event.preventDefault(); | |
scrollLocation = $( window ).scrollTop(); | |
window.location.hash = $( event.target ).attr('href').substr(1); | |
}); | |
$( window ).on( "hashchange", function( event ){ | |
event.preventDefault(); | |
$( window ).scrollTop( scrollLocation ); | |
if( typeof cb === "function" ){ | |
cb(); | |
} | |
}); | |
}; | |
hashThis( $( ".myAnchor" ), function(){ | |
// do something useful! | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment