如何使用jQuery找到元素px顶部的垂直距离 [英] how to find the vertical distance from top in px of an element using jQuery
本文介绍了如何使用jQuery找到元素px顶部的垂直距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用javascript / jQuery找到从页面顶部到DOM中元素所在位置的垂直距离?
How do I find the vertical distance from the top of the page to where the element exist in the DOM using javascript/jQuery?
我喜欢
<ul>
<li>one</li>
<li>one</li>
<li>one</li>
<li>one</li>
<li class="test">one</li>
....
....
....
<li>one</li>
</ul>
例如,我想找到从页面顶部到<$ c的垂直距离$ c> li #test 元素。
For example here, I want to find the vertical distance from top of the page to the li#test
element.
我试过 .scrollTop()
但是它始终为0!
I tried .scrollTop()
but it always comes as 0!
推荐答案
使用 .offset()
获取元素与文档顶部之间的距离:
Use .offset()
to get the distance between an element and the top of the document:
$("li.test").offset().top
这篇关于如何使用jQuery找到元素px顶部的垂直距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文