gpt4 book ai didi

javascript - 使用滚动查找所有元素

转载 作者:可可西里 更新时间:2023-11-01 01:26:12 25 4
gpt4 key购买 nike

查找页面上所有具有滚动条的元素的最可靠和最有效的方法是什么?


目前,我正在考虑使用 element.all()filter()比较 heightscrollHeight 属性值:

element.all(by.xpath("//*")).filter(function (elm) {
return protractor.promise.all([
elm.getAttribute("height"),
elm.getAttribute("scrollHeight")
]).then(function (heights) {
return heights[1] > heights[0];
});
});

但我不确定这种方法的正确性和性能。

最佳答案

这适用于水平和垂直滚动条。诀窍是检测太宽/太短以及计算的 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();

关于javascript - 使用滚动查找所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34532331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com