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.

Set block: nearest with offset?

See original GitHub issue

I need to be able to scroll into view with block: 'nearest' but with an offset. So this means I can have it scroll to the bottom of a div, but provide some buffer space below so the div isn’t on the exact bottom of the screen. The center option is too drastic for my use, I just need a buffer of 30 pixels or so but still want it at the bottom of the screen.

I’m hoping there is a way to do this, but if not then this would be a feature request 😃

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bluecaretcommented, May 9, 2018

Interesting, did not know that was a CSS option! Learn something new 👍 Thanks!

1reaction
stipsancommented, May 8, 2018

The best way to do that is by using CSS, add this next to where you are using overflow: scroll or overflow: auto:

scroll-behavior: smooth;

If that is not an option you can copy the logic the library is using internally to make smooth scrolling work without adding that CSS.

import scrollIntoView from "scroll-into-view-if-needed";

const supportsScrollBehavior =
  "scrollBehavior" in document.documentElement.style;

scrollIntoView(node, {
  behavior: actions =>
    actions.forEach(({ el, top: origTop, left }, i) => {
      // apply the 30px on the first scrolling box only
      const top = i === 0 ? origTop - 30 : origTop;

      // `Element.scroll` have historically worked the same way as `window.scrollTo`
      // Browsers like Safari does not support passing the new object variant
      // and that's why the extra `supportsScrollBehavior` check is necessary
      if (el.scroll && supportsScrollBehavior) {
        el.scroll({ top, left, behavior: "smooth" });
      } else {
        if (el === document.documentElement) {
          window.scrollTo(left, top);
        } else {
          el.scrollTop = top;
          el.scrollLeft = left;
        }
      }
    })
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set block: nearest with offset? · Issue #228 · scroll-into-view ...
I need to be able to scroll into view with block: 'nearest' but with an offset. So this means I can have it...
Read more >
offset - CSS: Cascading Style Sheets - MDN Web Docs
The offset CSS shorthand property sets all the properties required for animating an element along a defined path.
Read more >
VBA - Station Offset (alignment and block reference)
Solved: I am trying to link block reference data to the closest alignment and extract the station and offset from that alignment.
Read more >
Get position/offset of element relative to a parent container?
offsetTop give an element's position with respect to its offsetParent (which is the nearest parent element with a position of relative or ...
Read more >
Offset - MathWorks
The Offset block removes or keeps values from the beginning or end of the input vectors. The number of values to remove or...
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