w3c / csswg-drafts

CSS Working Group Editor Drafts
https://drafts.csswg.org/
Other
4.5k stars 667 forks source link

[css-scroll-snap] Programmatically scroll to snap points #8558

Open SebastianZ opened 1 year ago

SebastianZ commented 1 year ago

@Schepp mentioned in #156 that he'd like to have a way to programmatically scroll to a certain snap point.

There are indeed use cases in which authors may want more explicit control over the scroll position when scrolling with a scroll container. E.g. scrolling to a specific image in a gallery.

A possible solution for that would be to reuse the scrollTo() function for that. Its ScrollToOptions dictionary could be extended by a new option indicating a snap point.

Sebastian

johannesodland commented 1 year ago

This would be really useful 👍🏻

At the moment we have to infer the position of the snap points, which is somewhat cumbersome. It involves getting the following values:

fantasai commented 3 months ago

@johannesodland The various scrolling functions are supposed to honor the snap positions of the elements they're scrolling to, taking into account scroll-padding, scroll-margin, and scroll-snap-align. Why wouldn't that solve the use case here?

johannesodland commented 3 months ago

@fantasai You're right. scrollIntoView() with the block and inline options would get you close here. I still think you have to retrieve and parse the computed scroll-snap-align property of the element to figure out which options you should call scrollIntoView() with?

Maybe a new value for ScrollLogicalPosition could make this more ergonomic?

element.scrollIntoView({block: 'snap'})