Javascript jQuery从顶部获取高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11086141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery get the height from top
提问by Daniel Koczu?a
I need to get the height from top of the page to current scrollbar position and place it to my link:
我需要获取从页面顶部到当前滚动条位置的高度并将其放置到我的链接中:
<a class="poplight" rel="popup_name" href="#?w=here comes the value"></a>
How can I do this?
我怎样才能做到这一点?
回答by Rene Pot
Use the offset()
使用偏移量()
$('.poplight').offset().top
If you need to scroll to that position:
如果您需要滚动到该位置:
$('html, body').animate({
scrollTop: $('.poplight').offset().top
}, 400);
If you need the distance from the top of the window to the current position based on scroll:
如果您需要根据滚动从窗口顶部到当前位置的距离:
$(window).scrollTop()
Want to add that to the url:
想将其添加到网址:
$(".poplight").attr("href", "#" + $(window).scrollTop())