我有一个div,想找到最低位置.我可以找到这样的Div的顶部位置,但我如何找到底部位置?
var top = $('#bottom').position().top;
return top;
Run Code Online (Sandbox Code Playgroud)
use*_*361 148
将外部高度添加到顶部,并且相对于父元素具有底部:
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
Run Code Online (Sandbox Code Playgroud)
使用绝对定位的元素或相对于文档定位时,您需要使用偏移量来评估:
var bottom = $el.offset().top + $el.outerHeight(true);
Run Code Online (Sandbox Code Playgroud)
正如trnelson所指出的,这在100%的时间里都不起作用.要将此方法用于定位元素,还必须考虑偏移量.有关示例,请参阅以下代码.
var bottom = $el.position().top + $el.offset().top + $el.outerHeight(true);
Run Code Online (Sandbox Code Playgroud)
yoy*_*nno 13
编辑:此解决方案现在也在原始答案中.
接受的答案并不完全正确.您不应该使用position()函数,因为它是相对于父级的.如果您正在进行全球定位(在大多数情况下?),您应该只添加具有外部高度的偏移顶部,如下所示:
var actualBottom = $(selector).offset().top + $(selector).outerHeight(true);
Run Code Online (Sandbox Code Playgroud)
文档http://api.jquery.com/offset/
var bottom = $('#bottom').position().top + $('#bottom').height();
Run Code Online (Sandbox Code Playgroud)
如果您只想使用高度而不使用填充,边框等,那么到目前为止的答案都可以使用。
如果要考虑填充,边框和边距,则应使用.outerHeight
。
var bottom = $('#bottom').position().top + $('#bottom').outerHeight(true);
Run Code Online (Sandbox Code Playgroud)