Skip to content

Instantly share code, notes, and snippets.

@davidchase
Last active January 12, 2022 10:00
Show Gist options
  • Save davidchase/5445485 to your computer and use it in GitHub Desktop.
Save davidchase/5445485 to your computer and use it in GitHub Desktop.
simple hash change with scrolling, using jQuery

#Hash Change onScroll

Reminder:

Need to add mootools version too :)

Change the value of the url by scrolling through div's data-id or #

//uses on instead of bind, so it works with 1.9.1/2.0 +
$(document).on('scroll',function(e)
{
$('section').each(function()
{
if ( $(this).offset().top < window.pageYOffset + 10
&& $(this).offset().top +
$(this).height() > window.pageYOffset + 10
)
{
var data = $(this).data('id');
window.location.hash = data;
}
});
});
Copy link

ghost commented Jan 11, 2018

Works very well. Thank you.

@matteomanzo
Copy link

Thanks!

Copy link

ghost commented Jan 9, 2020

Great, thanks!

@zozobalogh0817
Copy link

Thanks a lot! You are the true guardian of the republic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment