我通过AJAX加载元素。其中一些只有当你向下滚动页面时才能看到。有什么方法可以知道元素现在是否在页面的可见部分?
当前回答
这是我的纯JavaScript解决方案,如果它隐藏在一个可滚动的容器。
演示在这里(尝试调整窗口的大小)
var visibleY = function(el){
var rect = el.getBoundingClientRect(), top = rect.top, height = rect.height,
el = el.parentNode
// Check if bottom of the element is off the page
if (rect.bottom < 0) return false
// Check its within the document viewport
if (top > document.documentElement.clientHeight) return false
do {
rect = el.getBoundingClientRect()
if (top <= rect.bottom === false) return false
// Check if the element is out of view due to a container scrolling
if ((top + height) <= rect.top) return false
el = el.parentNode
} while (el != document.body)
return true
};
编辑2016-03-26:我已经更新了解决方案,以考虑滚动过去的元素,所以它隐藏在可滚动容器的顶部。 编辑2018-10-08:更新到当滚动到屏幕上方的视图外时处理。
其他回答
我们可以在使用ES6的现代浏览器中做这样的事情:
const isFullySeen = el => el &&
typeof el.getBoundingClientRect === 'function' &&
el.getBoundingClientRect()['bottom'] + window.scrollY <=
window.innerHeight + window.scrollY &&
el.getBoundingClientRect()['top'] + window.scrollY <=
window.innerHeight + window.scrollY;
我已经为该任务编写了一个组件,旨在以极快的速度处理大量元素(在慢速手机上处理1000个元素的时间小于10ms)。
它适用于你可以访问的所有类型的滚动容器-窗口,HTML元素,嵌入式iframe,衍生子窗口-并且非常灵活地检测什么(完全或部分可见性,边框框或内容框,自定义容差区等)。
一个巨大的、主要自动生成的测试套件可以确保它像宣传的那样跨浏览器工作。
如果你喜欢,试试吧:jQuery.isInView。否则,你可能会在源代码中找到灵感,比如这里。
这将考虑元素的任何填充、边框或边距,以及大于视口本身的元素。
function inViewport($ele) {
var lBound = $(window).scrollTop(),
uBound = lBound + $(window).height(),
top = $ele.offset().top,
bottom = top + $ele.outerHeight(true);
return (top > lBound && top < uBound)
|| (bottom > lBound && bottom < uBound)
|| (lBound >= top && lBound <= bottom)
|| (uBound >= top && uBound <= bottom);
}
要调用它,可以使用这样的代码:
var $myElement = $('#my-element'),
canUserSeeIt = inViewport($myElement);
console.log(canUserSeeIt); // true, if element is visible; false otherwise
我在我的应用程序中有这样一个方法,但它不使用jQuery:
/* Get the TOP position of a given element. */
function getPositionTop(element){
var offset = 0;
while(element) {
offset += element["offsetTop"];
element = element.offsetParent;
}
return offset;
}
/* Is a given element is visible or not? */
function isElementVisible(eltId) {
var elt = document.getElementById(eltId);
if (!elt) {
// Element not found.
return false;
}
// Get the top and bottom position of the given element.
var posTop = getPositionTop(elt);
var posBottom = posTop + elt.offsetHeight;
// Get the top and bottom position of the *visible* part of the window.
var visibleTop = document.body.scrollTop;
var visibleBottom = visibleTop + document.documentElement.offsetHeight;
return ((posBottom >= visibleTop) && (posTop <= visibleBottom));
}
编辑:此方法适用于I.E.(至少版本6)。请阅读评论以了解FF的兼容性。
这个问题有超过30个答案,没有一个使用我一直在使用的令人惊讶的简单的纯JS解决方案。没有必要仅仅为了解决这个问题而加载jQuery,因为许多人都在推动这个问题。
为了判断元素是否在视口中,我们必须首先确定元素在主体中的位置。我们不需要像我以前想的那样递归地做这个。相反,我们可以使用element.getBoundingClientRect()。
pos = elem.getBoundingClientRect().top - document.body.getBoundingClientRect().top;
这个值是物体顶部和主体顶部之间的Y差。
然后我们必须判断元素是否在视图中。大多数实现都会询问完整的元素是否在视口中,所以这就是我们将要讨论的内容。
首先,窗口的顶部位置是:window. scrolly。
我们可以通过将窗口的高度加到窗口的顶部位置来获得窗口的底部位置:
var window_bottom_position = window.scrollY + window.innerHeight;
让我们创建一个简单的函数来获取元素的顶部位置:
function getElementWindowTop(elem){
return elem && typeof elem.getBoundingClientRect === 'function' ? elem.getBoundingClientRect().top - document.body.getBoundingClientRect().top : 0;
}
这个函数将返回元素在窗口中的顶部位置,或者如果你通过. getboundingclientrect()方法传递给它的不是元素,它将返回0。这个方法已经存在很长时间了,所以您不必担心浏览器不支持它。
现在,元素的顶部位置是:
var element_top_position = getElementWindowTop(element);
And或元素的底部位置为:
var element_bottom_position = element_top_position + element.clientHeight;
现在我们可以通过检查元素的底部位置是否低于viewport的顶部位置,以及检查元素的顶部位置是否高于viewport的底部位置来确定元素是否在viewport中:
if(element_bottom_position >= window.scrollY
&& element_top_position <= window_bottom_position){
//element is in view
else
//element is not in view
从那里,您可以执行逻辑来在元素上添加或删除视图内类,然后您可以稍后在CSS中使用过渡效果处理该类。
我非常惊讶,我没有在其他地方找到这个解决方案,但我相信这是最干净和最有效的解决方案,而且它不需要您加载jQuery!