This content originally appeared on CSS-Tricks and was authored by Chris Coyier
If you have a table of contents on a long-scrolling page, thanks to, say, position: fixed;
or position: sticky
;, the IntersectionObserver
API in JavaScript is the perfect companion to highlight items in the table of contents when corresponding content is in view.
Ben Frain has a post all about this:
Thanks to IntersectionObserver we have a small but very efficient bit of code to create our table of contents, provide quick links to jump around the document and update readers on where they are in a document as they read.
Compared to older techniques that need to bind to scroll events and perform their own math, this code is shorter, faster, and more logical. If you’re looking for the demo on Ben’s site, the article is the demo. And here’s a video on it:
I’ve mentioned this stuff before, but here’s a Bramus Van Damme version:
And here’s a version from Hakim el Hattab that is just begging for someone to port it to IntersectionObserver
because the UI is so cool:
Direct Link to Article — Permalink
The post Table of Contents with IntersectionObserver 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
Chris Coyier | Sciencx (2021-03-11T21:00:29+00:00) Table of Contents with IntersectionObserver. Retrieved from https://www.scien.cx/2021/03/11/table-of-contents-with-intersectionobserver/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.