Possible Duplicate:
How to tell if a DOM element is visible in the current viewport?
有一个HTML页面只有一个div,其中包含图像。
您如何检测该元素是否正在查看/是否在浏览器窗口中?
最佳答案
如果我理解正确,则想知道图像是否在视口中并且尚未滚动。在这种情况下,您需要获取视口和图像高度,获取图像的位置和文档的当前位置。如果您使用的是jquery(我假设来自标签),则可以执行以下操作:
var viewportHeight = $(window).height();
var imageOffset = $('img:first').offset().top;
var imageHeight = $('img:first').height;
var documentPosition = $(window).scrollTop();
var visibleArea = documentPosition + viewportHeight; //end of visible area
var imageArea = imageOffset + imageHeight;
if (documentPosition <= imageOffset && visibleArea >= imageArea) {
//image is entirely visible
}