2012-03-12 52 views
0

中心我的图像上的列表:http://johanberntsson.se/dev/fotosida/获取的图像是在屏幕

这将是一个很好的方式来获得图像这就是一个数据属性目前在画面的中心?在屏幕中央放置一条看不见的线,看看它与哪个图像碰撞?或者你有其他建议吗?

希望你有想法,否则生病详细。

谢谢!

+0

很酷的照片。这有帮助吗? http://stackoverflow.com/questions/8229291/how-to-check-if-an-element-is-in-the-view-of-the-user-with-jquery – 2012-03-12 19:47:24

+0

@gdoron我认为他意味着图像目前正在查看滚动 – 2012-03-12 19:47:47

+0

ALso http://stackoverflow.com/questions/2556168/how-to-detect-that-an-html-element-is-in-view – 2012-03-12 19:48:18

回答

0

你可以做这样的事情:

// finding the center point of the screen 
// note that $(window).height() returns the height of the viewport 
var center = Math.floor(($(window).height()/2)) + $(window).scrollTop(); 

// looping through images and checking each for being in middle 
$('.mainimages').each(function(i, img) { 
    var top = $(img).offset().top; 
    if(top <= center && top + $(img).height() >= center) { 
     // $(img) is in the center 
    } 
}); 

我不使用jQuery所以我不是,如果有更好的方法来使用要做到这一点肯定的。

另外,不是我不考虑图像之间的边距/填充。因此,如果您要在中心图像上放置彩色边框(在其他图像上没有边框),那么在图像之间会有图像没有边框的时刻。

+0

这正是我所需要的。我只是扩大了一点:)谢谢马歇尔! – Johan 2012-03-12 20:05:42