Comments (10)
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.
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.
Can you provide a link or share the code you're using?
from smooth-scroll.
@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.
Yeah sorry about that :) let me know when you get it working in pure JS so I can switch back. Cheers!
from smooth-scroll.
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.
@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.
@anderssonma - Perfect, thanks for testing.
from smooth-scroll.
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.
Happy to help!
from smooth-scroll.
Related Issues (20)
- not working on Firefox 70.0.1 / Ubuntu HOT 4
- Manual update: Scrolling to an anchor link on another page HOT 6
- Does not support special characters (Γ€,ΓΆ,Γ₯, etc.) HOT 1
- [Question] Draggable scrolling? HOT 3
- Offsetting for fixed header when coming from another page. HOT 2
- Polyfill for scroll-behavior? HOT 2
- Uncaught ReferenceError: SmoothScroll is not defined when compiling/minifying code into main scripts.min.js file HOT 2
- v16.1.1: Not published on npm! HOT 1
- Forgot console.log in last commit HOT 1
- Setting up auto offset HOT 9
- Scroll without #id_name on url HOT 1
- Focus is not adjusted when reduceMotion is enabled HOT 3
- Edge case issue - Failed to execute 'pushState' on 'History when linking to a from that has an input with name 'id' HOT 1
- overflow-y: scroll in css breaks smooth-scroll HOT 3
- Disable focus on options HOT 1
- Back button with header issue HOT 1
- Missing documentation step on how to import project via npm HOT 2
- Easing pattern documentation in source code is wrong
- Focus is not shifting properly when reduceMotion is enabled
- The event object is passed into the popHandler() function but not used
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from smooth-scroll.