Coder Social home page Coder Social logo

Comments (5)

bluecaret avatar bluecaret commented on August 26, 2024 2

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

from scroll-into-view-if-needed.

stipsan avatar stipsan commented on August 26, 2024 1

Hey!

Have you tried adding some CSS padding to your div?

If you look at the Custom Transition example on the demo and inline: "nearest" you can see how it's using a fixed width to apply an offset, it could just as well used padding to apply the offset:
image

If Element.scrollIntoView never scrolled more than one element (like document.body) an offset option would be reasonable to have.
It can be any number of elements though and I'm not sure how that would work. Should the offset apply to all of the scrolling boxes, or just window or document.body, or just the first one?

It's also possible for you to apply the offset yourself if you override the scrolling logic:

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

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

from scroll-into-view-if-needed.

bluecaret avatar bluecaret commented on August 26, 2024 1

Yes, I can't use padding, have to use margin, that's precisely my issue 😄

I tried the override and it works, but doesn't smooth scroll, just jumps to the position, I still need it to smoothly scroll to it. Any hope for that?

Thanks!

from scroll-into-view-if-needed.

stipsan avatar stipsan commented on August 26, 2024 1

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;
        }
      }
    })
});

from scroll-into-view-if-needed.

stipsan avatar stipsan commented on August 26, 2024

Margins are excluded though. If your current CSS and HTML structure prevents you from being able to use padding on your div for this then I hope the custom behavior snippet above can help you 😄

from scroll-into-view-if-needed.

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.