Skip to content

Instantly share code, notes, and snippets.

@dutchcelt
Last active February 11, 2024 20:37
Show Gist options
  • Save dutchcelt/7563661 to your computer and use it in GitHub Desktop.
Save dutchcelt/7563661 to your computer and use it in GitHub Desktop.
Hash location and stay put!
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