我用这个功能对这些类型的东西
// Returns true when element is mostly visible in the viewport
// -------------------------------
isElementInViewport: function (el) {
//special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
测试它像这样
if(isElementInViewport(yourElm)){
// elm is in viewport
}
,你会想要观看的滚动。让我知道你是否需要知道如何做到这一点。
谢谢!是的,如果你可以提供一个jsfiddle例子,它会非常有帮助。我对js不太了解,我宁愿不使用jquery,因为我听说它可能会减慢速度。 – Barbara 2014-10-11 10:37:19