I've been stuck on this for a few hours now and can't seem to find the issue. I'm also having issues describing the problem, since I'm not really sure where the problem really lies. I've looked all over stackoverflow and other sites to find the solution, but so far I haven't..
I'm trying to build a one-page scroller, using jQuery. My problem basically comes down to this. Whenever I view my page and manipulate the URL fragment by typing in the addressbar to an ID of another element on my page, it navigates to the element with the ID. But since I'm using a fixed nav-bar on top of my page, it displays part of the element below the fixed element.
Is there a way to catch keydown/keyup events from manipulating the addressbar so I can make sure the segment is displayed at the correct offset? Is there a way to create a default offset after changing the URL fragment? I've tried the event 'hashchange' on the window object, but this fires also when I update the location.href.hash value to whichever segment the user is viewing. I've got a default top offset on my body element to make the initial load work with the fixed element.
It seems that a lot of other onepage-websites ignore the URL fragment and don't update the url at all unless the navigation is not at the top of the website. So you won't be able to link directly to a specific part of the website, unless the navigation is on the left or on the right.
Part of my code:
$(window).load(function(){
var segment = window.location.hash;
if(segment != ""){
if($(segment) != []){
var originOfSegment = $(segment).offset().top - 64;
$("body, html").animate({"scrollTop": originOfSegment + "px"}, 0);
}
}
$("a[href*='#']").on("click", function(e){
if(e.preventDefault){
e.preventDefault();
}
else{
e.returnValue = false;
}
var segment = this.hash;
//navigateTo(segment, 500);
window.location.hash = segment;
});
$(window).on("scroll", function(){
var idOfSegment = $(currentSegment).attr("id");
window.location.hash = idOfSegment;
});
});
I'm probably not the first one with this issue, but I can't seem to find the answer to this specific problem.
It's easily reproducable even without using JavaScript to initially calculate and set the offset for the element referenced to with the URL fragment.
From a users perspective:
- User: I go to a website using an url and an url fragment.
- Userrequest: http://example.com/index.html#contact
- Script: The script receives the request, sees a URL fragment is used and calculates, then defines an offset. Succes!
- User: With this link I opened the contactform and submit some information.
- User: Next thing I want to do, is go back to the productoverview and use the addressbar to do so.
- Userrequest: http://example.com/index.html#products
- Script: The hashchange event gets triggered, but no keyup-event on 'enter' can be caught, which leads to an unknown source from which the event occurred. The hash can also be changed through scrolling from segment to segment. This results in an element positioned below the fixed navbar. Since the website is not refreshed, but instead loaded from cache, there's no load or ready event I can use to calculate and adjust the offset.