I wanted to change the url to that of the section id that is currently scrolled to. However my below code just breaks the scroll (it doesn't scroll at all).
$(document).scroll(function() {
$('section').each(function(){
if ($(this).offset().top) {
window.location.hash = $(this).attr('id');
}
});
});
<section id="a"></section>
<br><br><br><br><br><br><br>
<section id="b"></section>
<br><br><br><br><br><br><br>
<section id="c"></section>
<br><br><br><br><br><br><br>
<section id="d"></section>
Any help would be much appreciated.
Many thanks.
$(this).attr('id')it will return you the id like a, b, c, d but when you assign it to hash it suppose to start with #, so just concate it like'#' + $(this).attr('id')and then assign the value.location.hashautomatically adds the#.