HTMLElement.offsetTop
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2015å¹´7æâ©.
HTMLElement.offsetTop 为åªè¯»å±æ§ï¼å®è¿åå½åå
ç´ ç¸å¯¹äºå
¶ offsetParent å
ç´ çé¡¶é¨å
è¾¹è·çè·ç¦»ã
è¯æ³
topPos = element.offsetTop;
åæ°
topPos为è¿åçåç´ æ°ã
示ä¾
js
var d = document.getElementById("div1");
var topPos = d.offsetTop;
if (topPos > 10) {
// div1 è·ç¦»å®ç offsetParent å
ç´ çé¡¶é¨çè·ç¦»å¤§äº 10 px
}
è§è
| Specification |
|---|
| CSSOM View Module > # dom-htmlelement-offsettop > |
Browser compatibility
In compliance with the specification, this property will return null on Webkit if the element is hidden (the style.display of this element or any ancestor is "none") or if the style.position of the element itself is set to "fixed".
This property will return null on Internet Explorer (9) if the style.position of the element itself is set to "fixed". (Having display:none does not affect this browser.)