question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Offset with smooth scroll

See original GitHub issue

I 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:open
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
stipsancommented, Jan 22, 2020

Alright I’ve had a rethink. Right now if you give behavior a callback this library will simply forward everything to scroll-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 use scroll-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:

// Given this markup:
// <div id="app" style="height: 100vh; overflow: auto;">
//   <header style="position: fixed; left: 0; top: 0; right: 0; height: 50px" />
//   <div id="hero" />
//   <footer id="contact-us" />
// </div>

import scrollIntoView from "smooth-scroll-into-view-if-needed";
const node = document.getElementById("contact-us");

scrollIntoView(node, {
  behavior: actions =>
    actions.map(action => {
      // Ensure the offset is only applied
      // to the element that needs it.
      if (action.el.matches("#app")) {
        // Apply the minimum needed scroll padding
        // to stay underneath the sticky header.
        action.top = Math.max(50, action.top);
      }

      return action;
    })
});

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. The scroll-margin|padding spec is only used in scroll snapping as far as I know, I’m not aware of any browsers supporting it for scrollIntoView and .focus APIs yet but I’m keeping an eye out for when that happens 😄

1reaction
0xsvencommented, Sep 27, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found