这适用于水平和垂直滚动条。诀窍是检测太宽/太短,如果计算的CSS将允许您显示滚动条。
var ElementsWithScrolls = (function() {
var getComputedStyle = document.body && document.body.currentStyle ? function(elem) {
return elem.currentStyle;
} : function(elem) {
return document.defaultView.getComputedStyle(elem, null);
};
function getActualCss(elem, style) {
return getComputedStyle(elem)[style];
}
function isXScrollable(elem) {
return elem.offsetWidth < elem.scrollWidth &&
autoOrScroll(getActualCss(elem, 'overflow-x'));
}
function isYScrollable(elem) {
return elem.offsetHeight < elem.scrollHeight &&
autoOrScroll(getActualCss(elem, 'overflow-y'));
}
function autoOrScroll(text) {
return text == 'scroll' || text == 'auto';
}
function hasScroller(elem) {
return isYScrollable(elem) || isXScrollable(elem);
}
return function ElemenetsWithScrolls() {
return [].filter.call(document.querySelectorAll('*'), hasScroller);
};
})();
ElementsWithScrolls();
使用香草JS,你可以做这样的事情'Array.prototype.slice.call(document.querySelectorAll ('*'))。filter(function(el){return el.offsetHeight!== el.scrollHeight})'但是如果你在这个页面上执行它,你会看到一些没有滚动条的元素,所以我认为它不可靠。只是想评论一下,我还没有具体的答案。 – A1rPun
@AndrewTempleton不用担心,今天会审查和决定赏金。并感谢您的答案! – alecxe