I had created a page that had internal jumps to a particular section inside a div
, with specified ID. It is working fine (internally).
localhost/index.html#career-div
localhost/index.html#about-div
I have elements with IDs career-div
and about-div
.
When I tried to jump from another page (say contact.html
) to index.html
with hashtag, it did not jump to the particular section!!
<a href="index.html#career-div">Career</a>
<div class="container">
<div id="career-div" class="content-block-div">
</div>
</div>
The above code just loads the index
page as normal, but is not jumping to specified ID. I tried the name attribute also, but it did not work!
If I hit the enter key on the address bar, the page jumps like it should! I want this to smoothly scroll after switching pages.
The function I am using for smooth scroll towards the target and button class management is here:
$(function(){
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
if(this.hash == '#content-spacer')
{
$('#button-home').addClass('active');
$('#button-about').removeClass('active');
$('#button-services').removeClass('active');
$('#button-careers').removeClass('active');
$('#button-contact').removeClass('active');
}
else if(this.hash == '#about-spacer')
{
$('#button-about').addClass('active');
$('#button-home').removeClass('active');
$('#button-services').removeClass('active');
$('#button-careers').removeClass('active');
$('#button-contact').removeClass('active');
}
else if(this.hash == '#services-spacer')
{
$('#button-services').addClass('active');
$('#button-about').removeClass('active');
$('#button-home').removeClass('active');
$('#button-careers').removeClass('active');
$('#button-contact').removeClass('active');
}
else if(this.hash == '#career-spacer')
{
$('#button-careers').addClass('active');
$('#button-services').removeClass('active');
$('#button-about').removeClass('active');
$('#button-home').removeClass('active');
$('#button-contact').removeClass('active');
}
else if(this.hash == '#contact-spacer')
{
$('#button-contact').addClass('active');
$('#button-careers').removeClass('active');
$('#button-services').removeClass('active');
$('#button-about').removeClass('active');
$('#button-home').removeClass('active');
}
return false;
}
}
});
});