我用這個功能對這些類型的東西
// 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