jQuery从顶部获得高度

Dan*_*uła 9 javascript jquery height hyperlink

我需要从页面顶部到当前滚动条位置的高度并将其放到我的链接:

<a class="poplight" rel="popup_name" href="#?w=here comes the value"></a>
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Ren*_*Pot 17

使用偏移量()

$('.poplight').offset().top
Run Code Online (Sandbox Code Playgroud)

如果您需要滚动到该位置:

$('html, body').animate({
    scrollTop: $('.poplight').offset().top
}, 400);
Run Code Online (Sandbox Code Playgroud)

如果您需要基于滚动从窗口顶部到当前位置的距离:

$(window).scrollTop()
Run Code Online (Sandbox Code Playgroud)

想要将其添加到网址:

$(".poplight").attr("href", "#" + $(window).scrollTop())
Run Code Online (Sandbox Code Playgroud)