Scroll Shadows With JavaScript

Scroll shadows are when you can see a little inset shadow on elements if (and only if) you can scroll in that direction. It’s just good UX. You can actually pull it off in CSS, which I think is …


The post Scroll Shadows With JavaScript appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Chris Coyier

Scroll shadows are when you can see a little inset shadow on elements if (and only if) you can scroll in that direction. It’s just good UX. You can actually pull it off in CSS, which I think is amazing and one of the great CSS tricks. Except… it just doesn’t work on iOS Safari. It used to work, and then it broke in iOS 13, along with some other useful CSS things, with no explanation why and has never been fixed.

So, now, if you really want scroll shadows (I think they are extra useful on mobile browsers anyway), it’s probably best to reach for JavaScript.

Here’s a pure CSS example so you can see it work in all browsers except iOS Safari. Screenshots:

I’m bringing this up now because I see Jonnie Hallman is blogging about tit again. He mentioned it as an awesome little detail back in May. There are certain interfaces where scroll shadows really extra make sense.

Taking a step back, I thought about the solution that currently worked, using scroll events. If the scroll area has scrolled, show the top and left shadows. If the scroll area isn’t all the way scrolled, show the bottom and right shadows. With this in mind, I tried the simplest, most straight-forward, and least clever approach by putting empty divs at the top, right, bottom, and left of the scroll areas. I called these “edges”, and I observed them using the Intersection Observer API. If any of the edges were not intersecting with the scroll area, I could assume that the edge in question had been scrolled, and I could show the shadow for that edge. Then, once the edge is intersecting, I could assume that the scroll area has reached the edge of the scroll, so I could hide that shadow.

Clever clever. No live demo, unfortunately, but read the post for a few extra details on the implementation.

Other JavaScript-powered examples

I do think if you’re going to do this you should go the IntersectionObserver route though. Would love to see someone port the best of these ideas all together (wink wink).


The post Scroll Shadows With JavaScript appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Chris Coyier


Print Share Comment Cite Upload Translate Updates
APA

Chris Coyier | Sciencx (2021-10-05T22:40:21+00:00) Scroll Shadows With JavaScript. Retrieved from https://www.scien.cx/2021/10/05/scroll-shadows-with-javascript/

MLA
" » Scroll Shadows With JavaScript." Chris Coyier | Sciencx - Tuesday October 5, 2021, https://www.scien.cx/2021/10/05/scroll-shadows-with-javascript/
HARVARD
Chris Coyier | Sciencx Tuesday October 5, 2021 » Scroll Shadows With JavaScript., viewed ,<https://www.scien.cx/2021/10/05/scroll-shadows-with-javascript/>
VANCOUVER
Chris Coyier | Sciencx - » Scroll Shadows With JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/05/scroll-shadows-with-javascript/
CHICAGO
" » Scroll Shadows With JavaScript." Chris Coyier | Sciencx - Accessed . https://www.scien.cx/2021/10/05/scroll-shadows-with-javascript/
IEEE
" » Scroll Shadows With JavaScript." Chris Coyier | Sciencx [Online]. Available: https://www.scien.cx/2021/10/05/scroll-shadows-with-javascript/. [Accessed: ]
rf:citation
» Scroll Shadows With JavaScript | Chris Coyier | Sciencx | https://www.scien.cx/2021/10/05/scroll-shadows-with-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.