I am trying to make fixed link navigation fixed when scrolling to an anchor. Eliminating scroll to anchor is not a problem.
The problem is that my navigator is almost at the bottom of the screen. So what happens if you scroll down, the navigation will get a class fixed. When you click on the binding link, the script scrolls far to the binding block. I tried to compensate for navigation to the height of navigation. This works, but only when you click the same link a second time. The first click on the anchor link still makes the link too far!
I created a script to explain what happens → Fiddle
I personally believe that scrollTo and the simultaneous creation of a navigator interfere with each other.
Does anyone know what can do this?
What I have:
<div class="anchor-links">
<div class="anchor-wrapper">
<ul class="nav-list">
<li><a href="#description" class="goSmoothly">Product information</a></li>
<li><a href="#bundles" class="goSmoothly">Product bundles</a></li>
<li><a href="#reviews" class="goSmoothly">Reviews</a></li>
<li><a href="#related" class="goSmoothly">Related products</a></li>
</ul>
</div>
</div>
<div id="description" class="block">description</div>
<div id="bundles" class="block">bundles</div>
<div id="reviews" class="block">reviews</div>
<div id="related" class="block">related</div>
var fixmeTop = $('.anchor-links').offset().top;
$(window).scroll(function() {
var currentScroll = $(window).scrollTop();
if (currentScroll >= fixmeTop){
$('.anchor-links').addClass("sticky");
} else {
$('.anchor-links').removeClass("sticky");
}
});
$('.goSmoothly').click(function(event){
event.preventDefault();
$(this).addClass('active');
$('html,body').animate({
scrollTop: $(this.hash).offset().top - 100
}, 500);
});
source
share