原生Js判断元素是否隐藏

刚刚在看echo.js的源码,因此此问题延伸自echo.js中的一行代码chrome

var isHidden = function(element) {
    return (element.offsetParent === null);
};

通过本身实践确实能够使用这种方法来判断当前元素是否被隐藏,包括经过设置父元素为display:none以及本身自己为none的状况。可是若是是经过设置visibility:hidden则没法检测出。dom

overflow关于这个问题的讨论优化

http://stackoverflow.com/ques...code

除了上面的方法还有这种element

function isHidden(el) {
    var style = window.getComputedStyle(el);
    return (style.display === 'none')
}

这种方式也是须要手动判断visibility。不过貌似offsetParent的方法十分缓慢,即便是新的chrome也没法优化。get

相关文章
相关标签/搜索