2014-12-06 26 views
0

得到這個圖像列表。我想顯示距離瀏覽器頂部200px最接近的數字的data-id值。看到我向下滾動,我看到值740,然後756,然後788等根據頂部的位置獲取屬性值

<div id="counter" style="position:fixed;top:10px;left:10px;height:200px;width:200px;"></div> 


<figure style="height:200px;" data-id="740"></figure> 
    <figure style="height:200px;" data-id="756"></figure> 
    <figure style="height:200px;" data-id="788"></figure> 
    <figure style="height:200px;" data-id="920"></figure> 
+1

哪裏是你的代碼? JSfiddle吧? – 2014-12-06 11:44:49

+0

http://jsfiddle.net/skiweather/2uwpq1w0/ – mark 2014-12-06 12:02:20

回答

0

您可以使用。首先():第一從頁面上指定選擇選擇的第一要素。

例如

$("figure").first()

$("figure:first")

然後得到數據-ID屬性你可以添加:

$("figure:first").data("id")

+0

是的,但基於頁面上的滾動位置。這不是解決方案 – mark 2014-12-06 12:46:56

+0

我看,這可能會幫助你然後:http://stackoverflow.com/questions/17441065/how-to-detect-scroll-position-of-page-using-jquery – jonbaldie 2014-12-06 13:01:21