cferdinandi / smooth-scroll

A lightweight script to animate scrolling to anchor links.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Opposite Reaction than Intended

Amakaruk opened this issue · comments

My code is correct, but I'm getting the exact opposite response - when I click my anchor link I scroll back to its location instead of the id. Any ideas on why?

UPDATE: I think I didn't understand you correctly but i'll leave my replies here for the time being


It seems to be a flaw with offsetTop. Smooth-scroll uses anchor.offsetTop to get the scroll distance. The problem is that's not exactly how offsetTop works as far as I understand, or at least not always. From MDN:

offsetTop returns the distance of the current element relative to the top of the offsetParent node.

So the problem for me at least is that this will report 0 since the div is likely a direct child of a container of some sort with no margin in between. In my case it's a bootstrap .row and a .col

And then the scrolling distance becomes a negative int. For instance 0 - 500, and it scrolls you back up to the top.

I don't have time to pursue this any further right now but it works if you include jQuery and swap the lines 29-30 from:

var startLocation = window.pageYOffset;
var endLocation = anchor.offsetTop;

to this instead:

var startLocation = $(window).scrollTop();
var endLocation = $(anchor).offset().top;

Including jQuery kind of destroys the purpose here, but that'll have to do for me right now. Don't forget to load jQuery first!

Can you provide a link or share the code you're using?

@andersonma - sorry, that was intended for @Amakaruk.

I think your hunch about offsetTop is correct. I'm working on a solution that will climb up the DOM tree to provide the true position, with vanilla JS.

Yeah sorry about that :) let me know when you get it working in pure JS so I can switch back. Cheers!

I just pushed out version 2.8, with a function for getting a more accurate distance to the top. I've never been able to duplicate this issue myself, though, so I can't properly test it. @anderssonma or @Amakaruk - would you mind confirming that the latest version works as it's supposed to?

https://github.com/cferdinandi/smooth-scroll/archive/master.zip

@cferdinandi Tested now and it seems to work, good job! :) Usually it scrolls perfectly but sometimes its 20-30 pixels too high, could be my markup. But that'll do for now.

I had to run the function on document.ready otherwise nothing happens when I click. Most likely because I'm loading my scripts at the top right now and the script executes immediately. But it might be good to not to initialize until the document is guaranteed to be ready.

@anderssonma - Perfect, thanks for testing.

Thank you

On Wed, Dec 4, 2013 at 7:58 AM, Chris Ferdinandi
notifications@github.comwrote:

@anderssonma https://github.com/anderssonma - Perfect, thanks for
testing.


Reply to this email directly or view it on GitHubhttps://github.com//issues/11#issuecomment-29811135
.

Happy to help!