I have a website with only one page. In nav bar, there are a links which leads to certain part of page. I'm using this code to help me with smooth scrolling and changing active a tag in nav bar:
$(document).ready(function () {
$(document).on("scroll", onScroll);
// smoothscroll
$('a[href^="#"]').on('click', function (e) {
e.preventDefault();
$(document).off("scroll");
$('a').each(function () {
$(this).removeClass('active');
})
$(this).addClass('active');
var target = this.hash,
menu = target;
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top+2
}, 800, 'swing', function () {
window.location.hash = target;
$(document).on("scroll", onScroll);
});
});
});
function onScroll(event){
var scrollPos = $(document).scrollTop();
$('#nav-bar a').each(function () {
var currLink = $(this);
var refElement = $(currLink.attr("href"));
if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
$('#nav #nav-bar ul li a').removeClass("active");
currLink.addClass("active");
}
else{
currLink.removeClass("active");
}
});
}
Everything work fine. But I have one problem. If you click on one page from nav bar, for example "About us", in url appears "/#about" from anchor id.
I would like to remove it, because I have only one page and it doesn't looks good.