IntersectionObserver accepts several threshold values (#tilPost)

The IntersectionObserver API is a fairly new API that helps developers to figure out if an element is visibile or becomes visible. during scroll. It solves the problem of using costly operations like calling getBoundingClientRect in…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

The IntersectionObserver API is a fairly new API that helps developers to figure out if an element is visibile or becomes visible. during scroll. It solves the problem of using costly operations like calling getBoundingClientRect inside of scroll handlers which can lead to janky scrolling.

Dan Callahan wrote an article on this topic and even when I spent a lot of time digging IntersectionObservers (I give a talk about it) he mentioned something that I missed so far.

Usually you initialize an IntersectionObserver like this:

// this fires when:
//   1. The target begins entering the viewport (0 < ratio < 1).
//   2. The target fully leaves the viewport (ratio <= 0).
let observer = new IntersectionObserver(handler, {
    threshold: 0
});

The snippet above defines a single threshold for the IntersectionObserver. But it is also possible to define several thresholds!!!

// this fires when:
//   1. The target begins entering the viewport (0 < ratio < 1).
//   2. The target fully enters the viewport (ratio >= 1).
//   3. The target begins leaving the viewport (1 > ratio > 0).
//   4. The target fully leaves the viewport (ratio <= 0).
let observer = new IntersectionObserver(handler, {
  threshold: [0, 1]
});

I have no idea how I could miss that!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2017-08-06T05:00:00+00:00) IntersectionObserver accepts several threshold values (#tilPost). Retrieved from https://www.scien.cx/2017/08/06/intersectionobserver-accepts-several-threshold-values-tilpost/

MLA
" » IntersectionObserver accepts several threshold values (#tilPost)." Stefan Judis | Sciencx - Sunday August 6, 2017, https://www.scien.cx/2017/08/06/intersectionobserver-accepts-several-threshold-values-tilpost/
HARVARD
Stefan Judis | Sciencx Sunday August 6, 2017 » IntersectionObserver accepts several threshold values (#tilPost)., viewed ,<https://www.scien.cx/2017/08/06/intersectionobserver-accepts-several-threshold-values-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » IntersectionObserver accepts several threshold values (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2017/08/06/intersectionobserver-accepts-several-threshold-values-tilpost/
CHICAGO
" » IntersectionObserver accepts several threshold values (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2017/08/06/intersectionobserver-accepts-several-threshold-values-tilpost/
IEEE
" » IntersectionObserver accepts several threshold values (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/08/06/intersectionobserver-accepts-several-threshold-values-tilpost/. [Accessed: ]
rf:citation
» IntersectionObserver accepts several threshold values (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/08/06/intersectionobserver-accepts-several-threshold-values-tilpost/ |

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.