Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Opposite Reaction than Intended #11

Closed
Amakaruk opened this issue Dec 3, 2013 · 10 comments
Closed

Opposite Reaction than Intended #11

Amakaruk opened this issue Dec 3, 2013 · 10 comments
Labels

Comments

@Amakaruk
Copy link

Amakaruk commented Dec 3, 2013

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?

@marcussjoholm
Copy link

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.

@marcussjoholm
Copy link

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!

@cferdinandi
Copy link
Owner

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

@cferdinandi
Copy link
Owner

@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.

@marcussjoholm
Copy link

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

@cferdinandi
Copy link
Owner

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

@marcussjoholm
Copy link

@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.

@cferdinandi
Copy link
Owner

@anderssonma - Perfect, thanks for testing.

@Amakaruk
Copy link
Author

Amakaruk commented Dec 5, 2013

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
.

@cferdinandi
Copy link
Owner

Happy to help!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants