Javascript判断页面内元素是否可见的3种方法

判断网页中的Element元素是否可见,在Google上搜索得到3中方法:

var d = document.getElementById('someId');

1.  d.checkVisibility();

此方法目前只有 谷歌 105 版本 和 Firefox 106版本 以后的浏览器支持,Safari全系不支持。

2. 判断d.offsetParent 是否为null。

 if (d.offsetParent === null) { console.log(' the element currently is invisible'); }

原文连接:https://stackoverflow.com/questions/19669786/check-if-element-is-visible-in-dom

3. 判断offsetWidth和offsetHeight同时等于0

function isvisible(obj) {
  return obj.offsetWidth > 0 && obj.offsetHeight > 0;
}

 

这是来自jQuery的是否可见的实现

原文链接:https://stackoverflow.com/questions/4795473/check-visibility-of-an-element-with-javascript

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章