Reputation: 504
I have a fixed menu bar at top of the page and all menu links are anchor links on the same page. In short, it's a one page website divided in sections with anchor links as menu items.
I am using the javascript below for smooth slide to section when menu items are clicked.
$(document).ready(function() {
$("a.anchorLink").anchorAnimate()
});
jQuery.fn.anchorAnimate = function(settings) {
settings = jQuery.extend({
speed : 1100
}, settings);
return this.each(function(){
var caller = this
$(caller).click(function (event) {
event.preventDefault()
var locationHref = window.location.href
var elementClick = $(caller).attr("href")
var destination = $(elementClick).offset().top-60;
$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
window.location.hash = elementClick-60
});
return false;
})
})
}
Since the top menu height is 60 pixels, I need to subtract it from offset().top to ensure slide works properly. I also have to subtract in location hash else there is a jerky slide (in FF and IE, works fine in Chrome) once the slide on click in complete.
It works great but the problem is the URL is shown as http://www.site.com/#NaN instead of http://www.site.com/#linkname
Here is the full code - http://jsfiddle.net/85saK/1/. Unfortunately, it won't show you the URL after click which ends with #NaN instead of the actual link ID, e.g #store, #home or #contact.
Upvotes: 1
Views: 2541
Reputation: 11
Try:
var destination = $(elementClick).offset().top -60;
$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
//window.location.hash = elementClick
elementClick
});
Upvotes: 1