I was wondering how I can update the Hash in the URL when scrolling.
I use the following code to update the current state in my single page design.
function setActiveListElements(){
// Get the offset of the window from the top of page
var windowPos = $(window).scrollTop();
$('#primary-navwrapper').find('li a[href^="#"]').each(function () {
var anchorId = $(this);
var target = $(anchorId.attr("href"));
//console.log(target);
var offsetTop = target.position().top - offset;
//var offsetBottom = offsetTop + target.height();
//var top = sidebar.offset().top;
if (target.length > 0) {
//console.log(target.position().top + target.height());
//if(windowPos >= offsetTop && windowPos <= offsetBottom) {
if (target.position().top - offset <= windowPos && target.position().top + target.height() + offset > windowPos) {
$('#primary-navwrapper li a').removeClass("current");
anchorId.addClass("current");
}
}
});
}
This function is called on scroll. Now I came with the following solution:
function setActiveListElements(){
// Get the offset of the window from the top of page
var windowPos = $(window).scrollTop();
$('#primary-navwrapper').find('li a[href^="#"]').each(function () {
var anchorId = $(this);
var target = $(anchorId.attr("href"));
//console.log(target);
var offsetTop = target.position().top - offset;
//var offsetBottom = offsetTop + target.height();
//var top = sidebar.offset().top;
if (target.length > 0) {
//console.log(target.position().top + target.height());
//if(windowPos >= offsetTop && windowPos <= offsetBottom) {
if (target.position().top - offset <= windowPos && target.position().top + target.height() + offset > windowPos) {
$('#primary-navwrapper li a').removeClass("current");
anchorId.addClass("current");
window.location.hash = $(this).attr('href');
}
}
});
}
But when I scroll you will see the hash totally flip out and freeze. What can this be?