因此,我试图在视口中显示全屏区域时调用某些函数。假设我有7个部分,那么当某个部分位于视口中时,我希望发生一些事情(我有一个函数可以将这些部分捕捉到视口中,因此在视口中永远不会有多个部分,但是我正在尝试查找在视口中可见哪个部分)。
这是一个小提琴:http : //jsfiddle.net/h7Hb7/2/
function isInViewport() {
$("section").each(function () {
var $this = $(this),
wHeight = $(window).height(),
rect = $this.getBoundingClientRect(); // Error in console
// Borrowed from http://stackoverflow.com/a/7557433/5628
if (rect.top >= 0 && rect.bottom <= wHeight) {
console.log($this.attr("id") + "in viewport");
}
});
}
$(window).scroll(function () {
// Other functions are called inside the setTimeout function, can't remove
clearTimeout($.data(this, "scrollTimer"));
$.data(this, "scrollTimer", setTimeout(function () {
isInViewport();
}, 1200));
});
我不知道从哪里开始寻找,但我想这与每个功能有关。是每个功能带来问题吗?不可能是CSS问题,因为CSS加载后滚动时就会发生问题。
jQuery对象没有getBoundingClientRect
方法,您应该获取HTMLElement对象,然后调用该方法或:
this.getBoundingClientRect();
作为建议,如果可以选择使用插件,则可以考虑使用jquery.inview
插件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句