简体   繁体   English

使用纯JavaScript获取“ position:fixed”元素的Y位置

[英]Get the Y position of “position: fixed” element with pure javascript

Is there any way to get the current Y position of an element that has "position: fixed" set in CSS? 有什么方法可以获取在CSS中设置了“ position:fixed”的元素的当前Y位置? I just need to know the distance that this element is away from the top of the document in any way that uses pure javascript. 我只需要知道使用纯JavaScript的任何方式该元素与文档顶部之间的距离即可。

If that is not possible, I want to know why it is not possible in terms of logic. 如果这不可能,我想知道为什么在逻辑上不可能。

Thanks! 谢谢!

Use offsetTop property. 使用offsetTop属性。

 var d = document.querySelector('div'); console.log(d.offsetTop); 
 div{ position:fixed; top:50px; background:red; width:50px; height:50px; } 
 <div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM