// Determine if an element is in the visible viewport function isInViewport(element) { var rect = element.getBoundingClientRect(); var html = document.documentElement; //在IE中,默认坐标从(2,2)开始计算,导致最终距离比其他浏览器多出两个像素,我们需要做个兼容。 var top = document.documentElement.clientTop; // 非IE为0,IE为2 var left= document.documentElement.clientLeft; return ( rect.top -top >= 0 && rect.left -left >= 0 && rect.bottom -top <= (window.innerHeight || html.clientHeight) && rect.right -left <= (window.innerWidth || html.clientWidth) ); }
时间: 2024-11-06 13:01:48