Table of Contents with IntersectionObserver

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 …


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

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 ArticlePermalink


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Table of Contents with IntersectionObserver." Chris Coyier | Sciencx - Thursday March 11, 2021, https://www.scien.cx/2021/03/11/table-of-contents-with-intersectionobserver/
HARVARD
Chris Coyier | Sciencx Thursday March 11, 2021 » Table of Contents with IntersectionObserver., viewed ,<https://www.scien.cx/2021/03/11/table-of-contents-with-intersectionobserver/>
VANCOUVER
Chris Coyier | Sciencx - » Table of Contents with IntersectionObserver. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/11/table-of-contents-with-intersectionobserver/
CHICAGO
" » Table of Contents with IntersectionObserver." Chris Coyier | Sciencx - Accessed . https://www.scien.cx/2021/03/11/table-of-contents-with-intersectionobserver/
IEEE
" » Table of Contents with IntersectionObserver." Chris Coyier | Sciencx [Online]. Available: https://www.scien.cx/2021/03/11/table-of-contents-with-intersectionobserver/. [Accessed: ]
rf:citation
» Table of Contents with IntersectionObserver | Chris Coyier | Sciencx | 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.

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