但是我的情况我想添加一些函数当(只有一个)元素在视口上时点击一些html锚标记
时间:
示例:
块#A(TOP = 0,HEIGHT = 200)
块#B(TOP = 200,HEIGHT = 450)
块#C(TOP = 450,HEIGHt = 600)
当顶部视口 100 时,我将使用Block#A
执行某些操作到目前为止我的代码:
$(window).scroll(function() {
var elements = $('.block');
var len = elements.length;
for(var i=0;i<len;i++){
var element = elements[i];
var rect = element.getBoundingClientRect();
var statement = 'i have no idea';
if(statement) {
element.doSomething();
break;
}
}
});
答案 0 :(得分:4)
完成此事:
function ViewportDetect(el) {
var rect = el.getBoundingClientRect();
return rect.bottom > 0 && rect.top < (window.innerHeight || document.documentElement.clientHeight);
}