簡體   English   中英

使用純JavaScript獲取“ position:fixed”元素的Y位置

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

有什么方法可以獲取在CSS中設置了“ position:fixed”的元素的當前Y位置? 我只需要知道使用純JavaScript的任何方式該元素與文檔頂部之間的距離即可。

如果這不可能,我想知道為什么在邏輯上不可能。

謝謝!

使用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