3

I need the smooth scroll to be offset by 100px. So far I have this:

$(function() {
    $('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 1500, 'easeInOutExpo');
        event.preventDefault();
    });
});

This gets my link moving! But how can I add .offset().top > 100 to this? I am having issues figuring it out if anyone can help out.

1
  • Not sure I understand what you are asking. Create a jsfiddle or jsbin please. Commented Jul 3, 2015 at 8:14

2 Answers 2

1

Sounds like you have a fixed navbar. Assuming your anchors are empty:

<a class="anchor" id="foo"></a>

Just offset the anchors in css:

.anchor {
    top: -100px;
    position: relative;
    display: block;
}

Alternatively you can offset the scrollTop by updating your javascript:

scrollTop: $($anchor.attr('href')).offset().top - 100
Sign up to request clarification or add additional context in comments.

Comments

1

ve1jdramas Hi there.
Have a look at this link here.

In this example, they use + or - .

Like this... scrollTop: target.offset().top -100
or
scrollTop: target.offset().top +100

Try this and see if you can get this to work for you.
Or use this full code.

$(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 -100
            }, 1000);
            return false;
          }
        }
      });
    });

Comments

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.