我目前正在使用 getBoundingClientRect() 来确定元素是否进入视口。我真正需要做的是检查元素的 50%(或任何给定百分比)是否已进入视口(我正在检查滚动)。如果它是可见的,那么我更新页面上的一些文本说是,如果不是,那么文本说不。
我似乎无法理解这个逻辑,它开始让我发疯,有人能帮忙吗?
当前代码如下!
isBannerInView: function (el, y) {
var _this = this,
elemTop,
elemBottom,
elemHeight,
isVisible;
for (var i = 0; i < el.length; i++) {
var pos = banners.indexOf(el[i]);
elemTop = el[i].getBoundingClientRect().top;
elemBottom = el[i].getBoundingClientRect().bottom;
elemHeight = el[i].getBoundingClientRect().height;
isVisible = (elemTop >= 0) && (elemBottom <= window.innerHeight);
_this.updateResults(el[i], pos, isVisible);
};
},
updateResults: function (el, pos, isVisible) {
var isInView = isVisible ? 'Yes' : 'No';
document.querySelectorAll('.results')[0].getElementsByTagName('span')[pos].innerHTML = isInView;
},