[英]Get position of child element relative to side viewport
I have a parent with a horizontal overflow.我有一个水平溢出的父母。 I want the position of the children (and sub children) based on the (touch) scroll position relative to the left.
我想要基于相对于左侧的(触摸)滚动 position 的孩子(和子孩子)的 position。 Every JS function I tried returns fixed positions.
我试过的每个 JS function 都会返回固定位置。
<div class="parent"> <div class="child-one"></div> <div class="child-two"></div> </div>
.parent { overflow-x: scroll; overflow-y: hidden; width: 100vw; }.child-one, .child-two { width: 50vw; }
In this example, how to know when child-two enters the viewport on the right?在这个例子中,如何知道 child-two 何时进入右侧的视口? PS.
PS。 50vw is just an example, it's never at half the width of the parent:)
50vw 只是一个例子,它永远不会是父级宽度的一半:)
You need to look at the scrollLeft property ( https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft )您需要查看 scrollLeft 属性( https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft )
Basically, you will need to look at the absolute left of the child element (relative to the parent), subtract the width of the parent, and then if the scrollLeft property of the parent is larger than the difference, the child element will have scrolled into the visible range (and then you need to make sure it hasn't scrolled off the left as well)基本上,您需要查看子元素的绝对左侧(相对于父元素),减去父元素的宽度,然后如果父元素的 scrollLeft 属性大于差值,则子元素将滚动进入可见范围(然后您需要确保它也没有滚动到左侧)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.