简体   繁体   中英

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? 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.

If that is not possible, I want to know why it is not possible in terms of logic.

Thanks!

Use offsetTop property.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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