1

I have issue with fixed navigation and scrolling to element position. What this does is, if I click on the link ( anchor ) it scrolls slowly before element as i wish , but then it jumps a few pixels down. Problem is with -> $target.offset().top -50 // if I remove negative value ( -50 ) window scrolls to element but my fixed navigation cover the element little bit but there is no jump... and i if i add negative value window scrolls before element but then it's forced to jump down and I really don't know why....

$(".anchor").each(function () {
             $(this).click(function(e) {
            e.preventDefault();
            var target = this.hash;
                $target = $(target);

                $('html, body').animate({
                    scrollTop:  $target.offset().top -50
                }, 2000, function () {
                        window.location.hash = target;
                 });
            });
        })

1 Answer 1

1

The problem is not the -50 offset; the problem is about setting the hash after the animation is completed. That will make the page jump to the original hash position. You need to remove adding the hash when the animation is completed:

$(".anchor").each(function () {
    $(this).click(function(e) {
        e.preventDefault();
        var target = this.hash;
        $target = $(target);
        $('html, body').animate({
            scrollTop:  $target.offset().top - 50
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much : )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.