有可能是在你想要的形象标识,并做进一步处理的两个方案,
第一种情形你想执行的窗口滚动的东西。 在这种情况下,只需在滚动事件中添加一个处理程序。
$(window).scroll(function() {
var windowTop = $(this).scrollTop(),
image = $('#listimages').find('img').filter(function(){
return $(this).offset().top < windowTop+100;
//i am adding 100 in windowTop as we can consider the the image as cuurent image even if it is little bit below than top of window.
});
//now you can directly use image if you want to manipulate it.
//if you want id you can get it by
var id=image[0].id //or image.attr('id');
});
第二种情况,如果你想对任何事件的触发执行某些操作。
function currentImg(){
var windowTop = $(this).scrollTop(),
image = $('#listimages').find('img').filter(function(){
return $(this).offset().top < windowTop+100;
});
return image[0].id;
}
但是请记住添加像scroll,mousemove这样的事件会更频繁地执行,所以建议在需要它之前不要使用太多的东西。
你想在'滚动'的'ID'?或“悬停”? – 2013-03-07 04:02:02
如果所有图像都适合一个页面并且不需要滚动,该怎么办? – 2013-03-07 04:11:04
重复http://stackoverflow.com/questions/5911138/jquery-trigger-function-when-element-is-in-viewport – TheHe 2013-03-07 04:16:24