我需要获取从页面顶部到当前滚动条位置的高度并将其放置到我的链接中:
<a class="poplight" rel="popup_name" href="#?w=here comes the value"></a>
我该怎么做?
最佳答案
使用偏移量()
$('.poplight').offset().top
如果您需要滚动到该位置:
$('html, body').animate({
scrollTop: $('.poplight').offset().top
}, 400);
如果需要基于scroll的窗口顶部到当前位置的距离:
$(window).scrollTop()
想要将其添加到 url:
$(".poplight").attr("href", "#" + $(window).scrollTop())
关于javascript - jQuery 从顶部获取高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11086141/