Offset with smooth scroll
See original GitHub issueI am trying to get a smooth scroll to an element with a sticky navigation bar.
Overriding behavior (to add an offset) also overrides the smooth scroll. Can someone give me some advice on how to make this work?
const scrollIntoViewSmoothly =
'scrollBehavior' in document.documentElement.style
? scrollIntoView
: smoothScrollIntoView
scrollIntoViewSmoothly(node, {
block: 'start',
behavior: (actions) =>
actions.forEach(({ el, top, left }, i) => {
el.scrollTop = i === 0 ? top - 90 : top
el.scrollLeft = left
}),
})
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How to Set Offsett for Smooth Scroll - javascript - Stack Overflow
Approach: We get the height of fixed nav using document.getElementById('header').offsetHeight And offset the scroll to this value.
Read more >Pure CSS smooth scroll with offset - CodePen
Insecure Resource. The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https....
Read more >Smooth Scroll Offset Anchor Links with CSS - Spigot Design
A couple of quick CSS snippets that allow for smooth scrolling anchor links, that also offset for fixed headers. This is the one...
Read more >Smooth scroll component - UIkit documentation
The offset-option adds a specified distance to the target when calculating the scroll position. The offset is passed via the data-attribute. The value...
Read more >Smooth scrolling to a given offset in React Virtualized · GitHub
Smoothly animate between two values. * @param {Number} fromValue - the initial value. * @param {Function} onUpdate - A function that is called...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Alright I’ve had a rethink. Right now if you give
behavior
a callback this library will simply forward everything toscroll-into-view-if-needed
and no animation happens unless the browser can do it natively, which is confusing and not very helpful since if that’s what you need you should just usescroll-into-view-if-needed
directly instead of this library.I’ll make a new major release that lets you map over the coordinates when you provide a custom behavior, applying offsets as needed:
That’ll solve these kind of challenges for now. In the long run I want to extend compute-scroll-into-view itself to support either https://css-tricks.com/almanac/properties/s/scroll-padding/ directly, or provide the API needed to build it in packages like
scroll-into-view-if-needed
. Thescroll-margin|padding
spec is only used in scroll snapping as far as I know, I’m not aware of any browsers supporting it forscrollIntoView
and.focus
APIs yet but I’m keeping an eye out for when that happens 😄So I have worked around it by giving the element the style rule:
padding-top: 90px
and the element before the style rule:margin-bottom: -90px