用jQuery查找div底部的位置 [英] Finding the position of bottom of a div with jquery
问题描述
我有一个div,想找到最底端的位置.我可以这样找到Div的顶部位置,但是如何找到底部位置?
I have a div and want to find the bottom position. I can find the top position of the Div like this, but how do I find the bottom position?
var top = $('#bottom').position().top;
return top;
推荐答案
将外部高度添加到顶部,相对于父元素,您具有底部:
Add the outerheight to the top and you have the bottom, relative to the parent element:
var $el = $('#bottom'); //record the elem so you don't crawl the DOM everytime
var bottom = $el.position().top + $el.outerHeight(true); // passing "true" will also include the top and bottom margin
使用绝对定位的元素或相对于文档定位时,您将需要使用offset来求值:
With absolutely positioned elements or when positioning relative to the document, you will need to instead evaluate using offset:
var bottom = $el.offset().top + $el.outerHeight(true);
如 trnelson 所指出的那样,这在100%的时间内都无效.要对定位的元素使用此方法,还必须考虑偏移量.有关示例,请参见以下代码.
As pointed out by trnelson this does not work 100% of the time. To use this method for positioned elements, you also must account for offset. For an example see the following code.
var bottom = $el.position().top + $el.offset().top + $el.outerHeight(true);
这篇关于用jQuery查找div底部的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!