2012-06-18 29 views
8

我需要從頁面頂部的高度,以當前滾動條的位置,並將它放置我的鏈接高度:jQuery的獲得高層

<a class="poplight" rel="popup_name" href="#?w=here comes the value"></a> 

我怎樣才能做到這一點?

回答

15

使用OFFSET()

$('.poplight').offset().top 

如果您需要滾動到該位置:如果您需要根據滾動從窗口到當前位置的頂部的距離

$('html, body').animate({ 
    scrollTop: $('.poplight').offset().top 
}, 400); 

$(window).scrollTop() 

想將它添加到URL:

$(".poplight").attr("href", "#" + $(window).scrollTop()) 
+0

不,不。我必須將px的高度從站點頂部放置到當前視圖。例如,您將網站滾動到一半,我需要從頂部到當前視圖的高度並將其放到我的鏈接中。 –

+0

好的,我添加了它 –

+0

是的!而已。而現在,如何將px的毒品投放到我的鏈接? –