简体   繁体   English

获取子元素相对于侧视口的 position

[英]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.

相关问题 获取相对于视口顶部的元素位置 - Get an element position relative to the top of the viewport 相对于视口角度6的位置元素 - Position element relative to viewport angular 6 使用jquery获取元素相对于视口的位置 - Using jquery to get element's position relative to viewport 如何获取元素相对于浏览器视口的顶部 position? - How to get an element's top position relative to the browser's viewport? 如何获取元素相对于broswer视口的位置 - How can I get the position of an element relative to the broswer viewport 如何获得内部SVG元素相对于外部SVG元素视口的位置? - How do I get an internal SVG element's position relative to the viewport of an outer SVG element? 获取相对于父项的位置,而不是在mousedown / touchstart上的视口 - Get position relative to parent not viewport on mousedown / touchstart 查找元素相对于视口或文档的整数像素位置:getBoundingClientRect? - Find the integer pixel position of an element relative to the viewport or document: getBoundingClientRect? 通过Angular 2获取本机元素相对于视口的位置 - Getting position of native element relative to viewport via Angular 2 通过JS与视口(非相对)相关的元素的位置坐标 - Position coordinates of an element related to viewport (not relative) via JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM