Last active
September 6, 2018 13:15
-
-
Save xjinza/e369062711d98cca3ca4f21de43e7ba2 to your computer and use it in GitHub Desktop.
判断元素是否显示 原生js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
判断元素是否显示 | |
function isHidden(el) { | |
return (el.offsetParent === null) | |
} | |
#### HTMLElement.offsetParent | |
HTMLElement.offsetParent 是一个只读属性,返回一个指向最近的(closest,指包含层级上的最近)包含该元素的定位元素。如果没有定位的元素,则 offsetParent 为最近的 table, table cell 或根元素(标准模式下为 html;quirks 模式下为 body)。 | |
当元素的 style.display 设置为 "none" 时,offsetParent 返回 null。 | |
offsetParent 很有用,因为 offsetTop 和 offsetLeft 都是相对于其内边距边界的。 | |
在 Webkit 中,如果元素为隐藏的(该元素或其祖先元素的 style.display 为 "none"),或者该元素的 style.position 被设为 "fixed",则该属性返回 null。 | |
在 IE 9 中,如果该元素的 style.position 被设置为 "fixed",则该属性返回 null。(display:none 无影响。) | |
兼容性chrome、safari兼容性未知,ie、firefox可以兼容 | |
参考:https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/offsetParent | |
https://stackoverflow.com/questions/19669786/check-if-element-is-visible-in-dom |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment