我有一个div,想要找到最下面的位置.我可以这样找到Div的顶部位置,但是如何找到底部位置呢?
var top = $('#bottom').position().top;
return top;
我有一个div,想要找到最下面的位置.我可以这样找到Div的顶部位置,但是如何找到底部位置呢?
var top = $('#bottom').position().top;
return top;
将外部高度添加到顶部,您就得到了相对于父元素的底部:
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
对于绝对定位的元素或相对于文档定位时,您将需要改用偏移量进行计算:
var bottom = $el.offset().top + $el.outerHeight(true);
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);