Comments (5)
Interesting, did not know that was a CSS option! Learn something new 👍
Thanks!
from scroll-into-view-if-needed.
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:
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.
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.
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.
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)
- [BUG] Safari on macOS Monterey 12.3.1
- "Cannot use import statement outside a module" error when using v2.2.31 HOT 1
- Dealing with a Fixed Navbar HOT 2
- "export 'default' (imported as 'compute') was not found in 'compute-scroll-into-view' HOT 1
- t.scroll is not a function HOT 1
- [TS] typedoc TS1479 error HOT 8
- scrollIntoView does not work anymore for element in shadow dom
- something is wrong with the [email protected] project HOT 11
- scroll position error with `display: none;` element HOT 1
- Add an options to skip in document check or pass a new `document`
- Body behind the modal scrolls HOT 1
- Snaps instead of scrolling smoothly. HOT 2
- Unneeded scrolling of ancestors HOT 1
- how to know when scrolling is complete?
- Does not scroll to element in iframe outside viewport HOT 2
- How can I combine behavior function and behavior smooth
- Shadow DOM support? HOT 2
- Dependency Dashboard
- Scroll horizontally only inside a specific div HOT 1
- Horizontal RTL scrolling goes wrong way HOT 5
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 scroll-into-view-if-needed.