2016-08-10 172 views
2

我想计算从顶部到我添加链接的点的高度。如何使用jquery计算高度

如何计算的例子中我们使用高度:

w = $(window).height(); 

计算窗口的高度。

同样地,我要计算的锚定标记

<a href="#" id="calc"></a> 

从集管的地步补充说明的是锚定件的高度?

outerh = $('#calc').outerHeight(); 
innerh = $('#calc').innerHeight(); 

我在上面的代码尝试,但它返回的高度18px因为它计算的是锚标记的高度。

+0

offset()。top对你有帮助..但是如果你在这里写了一些你的HTML代码,那么我可以给出准确的答案。 –

回答

6

您可以使用offset().top来计算它。

获取第一个元素的当前坐标或设置匹配元素集合中每个元素相对于文档的坐标。

演示:从顶部

console.log($('#calc').offset().top);
body { 
 
    margin:0; 
 
} 
 

 
a { 
 
    display:inline-block; 
 
    margin-top:100px; 
 
}
<script src="https://code.jquery.com/jquery-3.0.0.js"></script> 
 
<a href="#" id="calc">link</a>

+0

我正在使用以下代码来调整我的弹出窗口acc。到高度h = $('。calc')。offset()。top w = $(window).height(); \t alert(h);警报(W); \t var cal =(w-h)/ 2; \t alert(cal); ('。pop')。css('margin-top',(w-h)/ 2 +“px”); h = $('。calc')。offset()。top;高度会因为在列表格式中提及而有所不同。 我怎样才能在中心显示弹出窗口? – user6617474

0

身高用于从锚定标记从顶部

height_top= $('#calc').offset().top; 

高度为从底部边缘锚标签的顶部边缘定位标记锚标记

height_top= $('#calc').offset().top + $('#calc').outerHeight();