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

 

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