javascript - Intersection observer thresholds value not working - Stack Overflow

Thresholds option in Intersection Observer doesn't seem to work.To give more context, my list item

Thresholds option in Intersection Observer doesn't seem to work.

To give more context, my list items are rendered in viewport and I want each item to observe on viewport's Intersection observer. The thing is, I need to observe them in steps of 25%. Both when they get in viewport and/or go out of viewport.

As per documentation it seems that providing thresholds array as [0, 0.25, 0.50. 0.75, 1] should do the work, but actually it doesn't make any difference than giving it a value of 0 or 1. I want to observe changes everytime an element's intersection increases or decreases by 25% in my scroll view.

You can check my code on -

I am using chrome v72 on Windows 10.

Thresholds option in Intersection Observer doesn't seem to work.

To give more context, my list items are rendered in viewport and I want each item to observe on viewport's Intersection observer. The thing is, I need to observe them in steps of 25%. Both when they get in viewport and/or go out of viewport.

As per documentation it seems that providing thresholds array as [0, 0.25, 0.50. 0.75, 1] should do the work, but actually it doesn't make any difference than giving it a value of 0 or 1. I want to observe changes everytime an element's intersection increases or decreases by 25% in my scroll view.

You can check my code on - https://codesandbox.io/s/rm46m1qy7o

I am using chrome v72 on Windows 10.

Share Improve this question edited Apr 3, 2021 at 2:06 Arjun 1,3951 gold badge12 silver badges28 bronze badges asked Feb 17, 2019 at 18:48 Vineet 'DEVIN' DevVineet 'DEVIN' Dev 1,2231 gold badge11 silver badges37 bronze badges 4
  • 1 I believe the constructor option is threshold. Like this: threshold: [0, 0.25, 0.5, 0.75, 1], (It's pretty confusing that the constructor option is different from the read-only thresholds property) – David784 Commented Feb 17, 2019 at 19:00
  • @David784 - Is there a readonly property named thresholds too? I was asking of the option we need to provide in constructor. – Vineet 'DEVIN' Dev Commented Feb 17, 2019 at 19:06
  • 1 Yeah, here's a link to the constructor parameters for IntersectionObserver on MDN. And here's one to the properties section. Notice the difference. Anyway, when I changed your codesandbox to threshold: it started working fine for me. – David784 Commented Feb 17, 2019 at 19:14
  • Oh thanks. I didn't notice the ReadOnly tag there before :). And actually its very easy to make that mistake as generally I name any array variable as plural. – Vineet 'DEVIN' Dev Commented Feb 17, 2019 at 19:29
Add a ment  | 

2 Answers 2

Reset to default 2

For anyone else ing here for their threshold value not working, I'll mention another quirk that confused me for a second ....

If it's just one value, it should be a primitive data type. (ie {threshold: 0.1}) If you have just one value and put it in an array (ie {threshold: [0.1]}), it won't work.

But multiple values need an array.

Answer is rather simple. thresholds is a Read-Only property. What we pass in constructor is threshold. Credits to @David784 for answering this.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745645403a4637940.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信