From 4b75449925036036b4d2272b2010cf516783729d Mon Sep 17 00:00:00 2001 From: Dave Bitter Date: Thu, 18 Nov 2021 16:29:29 +0100 Subject: [PATCH] feat(quick-bit): Add scroll snap article --- content/articles/quickBits.md | 59 ++++++++++++++++++++++++++ public/img/quick-bits/scroll-snap.png | Bin 0 -> 4083 bytes 2 files changed, 59 insertions(+) create mode 100644 public/img/quick-bits/scroll-snap.png diff --git a/content/articles/quickBits.md b/content/articles/quickBits.md index 59b88d0..43e87f3 100644 --- a/content/articles/quickBits.md +++ b/content/articles/quickBits.md @@ -1,5 +1,64 @@ --- items: + - type: quick-bits + body: >- + The idea is simple; the user scrolls. As soon as the user is over a certain threshold, you snap to the next item. We used to do this with Javascript to calculate when the user is over this threshold. We would then manually scroll to a certain position with a neat animation. + + + Besides this code becoming complex, it relies heavily on Javascript (that needs to be loaded). Next to that, you'd need to take certain aspects into account like cancelling the scroll snap if the user changes scroll direction. It would be great if the browser can handle all of this for us. Luckily it can! + + ## CSS scroll snap + + [The CSS scroll snap API](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Scroll_Snap) offers this for the web. In essence, you need two things. A container that needs to be scrolled in and items to snap to. Firstly, you add the `scroll-snap-type` property to the container: + + + ```css + + main { + height: 100vh; + overflow-y: auto; + scroll-snap-type: y mandatory; + } + + ``` + + + `y` is the direction you want to snap to. In this example, we want to snap while the user is scrolling vertically. `mandatory` is one of the values you can use to always snap: + + + *The visual viewport of this scroll container will rest on a snap point if it isn't currently scrolled. That means it snaps on that point when the scroll action finished, if possible. If content is added, moved, deleted or resized the scroll offset will be adjusted to maintain the resting on that snap point.* + + + Next, we can tell the items where we would like to align the scroll snapping to. You can do this by using the `scroll-snap-align` property on the item: + + + ```css + + section { + scroll-snap-align: center; + } + + ``` + + And that's it! This is all we need to implement scroll snapping with CSS only. Have a look at the interactive demo over at [Codepen](https://codepen.io/davebitter/full/oNeJQJw) or below. Thanks for reading! + +

+ See the Pen + Full-page scroll snap by Dave Bitter (@davebitter) + on CodePen. +

+ + + date: 2021-11-18T00:00:00.000Z + slug: css-scroll-snap + tags: + - front-end + intro: >- + Scroll snapping is a popular technique to focus on a single piece of information. How can we implement this without the need and complexity of Javascript-based solutions? + teaserCopy: >- + Scroll snapping is a popular technique to focus on a single piece of information. How can we implement this without the need and complexity of Javascript-based solutions? + teaserImage: /img/quick-bits/scroll-snap.png + title: Full page scroll snapping with CSS only - type: quick-bits body: >- Full disclaimer, I came across this solution watching a 45-second video by Fireship on Youtube which you can watch [here]([https://www.youtube.com/watch?v=ITogH7lJTyE](https://www.youtube.com/watch?v=ITogH7lJTyE)). All credits to them. I'm a fan of reading these at my own pace, how impressive 45 seconds is though, so here it is in writing. diff --git a/public/img/quick-bits/scroll-snap.png b/public/img/quick-bits/scroll-snap.png new file mode 100644 index 0000000000000000000000000000000000000000..978580c761e3696db2e163ef6bc26fdee6692ec9 GIT binary patch literal 4083 zcmeHKYdnF|v?#&o8&9Gfk6y>&6(-31YQzmRFm$FN?R5n8G zoou;waVgi`IZ?Tkq(RD{n-pp!HDr(KRKIgRo%7lA;eDTHt@U63XZ_cDKfI|P?p$p! z1PlOxwzCs^BLJu;0D$Uo4OOK@EPk|0`C7tfx-kLZLb~Q`&{E|YB-+Sz1j;*2MwNSM zh?kGp$IX?pT_`{W?GSF`p(KI`WfTC=BoyUVz!L|-C4#VU5k<1r{8I!)xnD4&&EcOy z#Gz}=ecU|YEMXK6PC(hC?9J(5I2=xk+Odck=y$IFgl-b?qF6$Kl2c6g+#bbKat8~=U%=lxzW{0Ih0?#1;f>s)9B#Jr^$)R7FT>LPz>^3+57lYBtWGd(2fs>Q6$z5#rDiFvR z1UiXAl@p0}YHCsr=b^K6yQAYxCbPxC;S3f_*3#z!!D^}0ekZ3266r3RT|=SdV=#48YB7OON+ez)laC>h-bO}s zG+HN%Re;6b=Wq_&+8(gBzQ^J0x3+fG)BDxVF4ofWBAL8VU%#A0l2}+IT3cVGQd1BJ zDVICo}xAoqi6FuOyKU+Su@*&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