Code Monkey home page Code Monkey logo

Comments (10)

marcussjoholm avatar marcussjoholm commented on August 17, 2024

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.

from smooth-scroll.

marcussjoholm avatar marcussjoholm commented on August 17, 2024

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!

from smooth-scroll.

cferdinandi avatar cferdinandi commented on August 17, 2024

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

from smooth-scroll.

cferdinandi avatar cferdinandi commented on August 17, 2024

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

from smooth-scroll.

marcussjoholm avatar marcussjoholm commented on August 17, 2024

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

from smooth-scroll.

cferdinandi avatar cferdinandi commented on August 17, 2024

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

from smooth-scroll.

marcussjoholm avatar marcussjoholm commented on August 17, 2024

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

from smooth-scroll.

cferdinandi avatar cferdinandi commented on August 17, 2024

@anderssonma - Perfect, thanks for testing.

from smooth-scroll.

Amakaruk avatar Amakaruk commented on August 17, 2024

Thank you

On Wed, Dec 4, 2013 at 7:58 AM, Chris Ferdinandi
[email protected]:

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

from smooth-scroll.

cferdinandi avatar cferdinandi commented on August 17, 2024

Happy to help!

from smooth-scroll.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.