kswedberg / jquery-smooth-scroll

Automatically make same-page links scroll smoothly
MIT License
1.97k stars 427 forks source link

Offset issue due to fixed navbar after scroll #88

Closed con322 closed 8 years ago

con322 commented 8 years ago

Is there a quick fix/options that so that after the navbar is fixed the offset is changed?

kswedberg commented 8 years ago

you could use the $.smoothScroll() utility method in a click handler and pass an offset option based on the position of the navbar. For example (you'll have to replace selectors, of course):

$('.some-class').on('click', function() {

  var $navBar = $('#navbar');

  $.smoothScroll({
    scrollTarget: this.hash,
    offset: $navBar.css('position') === 'fixed' ? -$navBar.height() : 0
  });
});
con322 commented 8 years ago

when i try the above code obviously changing the selectors I get the following error any ideas?

Uncaught TypeError: jQuery.smoothScroll is not a function 
kswedberg commented 8 years ago

The only thing I can think of is that maybe you're not referencing the plugin correctly before you call jQuery.smoothScroll? Do you have a URL I can look at to investigate?

con322 commented 8 years ago

Here's link atm it works (not using your code using the below code) but you'll see first click on the links blow the page header scroll to the correct position but if you click the link again when not at the top it changes position.

http://middlers.plewscreative.co.uk/solutions-for-you/individuals/

    smoothScroll.init({
        offset: 190, // Integer. How far to offset the scrolling anchor location in pixels
    });
kswedberg commented 8 years ago

ok, it looks like you're using someone else's plugin. Maybe this one?… https://github.com/cferdinandi/smooth-scroll

In any case, you'd probably want to change that offset to -190.

con322 commented 8 years ago

Apologies I was testing different ones, have your locally and get the following error any ideas?

Uncaught TypeError: Cannot read property 'smoothScroll' of undefined
kswedberg commented 8 years ago

If you're calling $.smoothScroll and getting that error, it means that $ is undefined. So, you either haven't loaded jQuery or it's in noConflict mode or some other situation where $ is not aliased to jQuery. So, if you're sure jQuery is loaded, try jQuery.smoothScroll instead.

kswedberg commented 8 years ago

@con322 : are you able to close this issue now, or do you need more help from me? I checked your site, and it looks like it's working. But please let me know if it's not.

kswedberg commented 8 years ago

Closing this due to lack of activity. If you need it re-opened or still have a question, please let me know.