我有一个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);

Jquery相关问答推荐

JQuery UI-AutoComplete未显示下拉菜单

添加动态表单后,Select2 消失

如何使 Bootstrap 轮播滑块使用移动左/右滑动

如何在 JavaScript 或 jQuery 中过滤 JSON 数据?

使用 jQuery,当用户仍在编辑该字段时,如何将文本字段的第一个字母大写?

扩展 jQuery 插件的最佳方式

了解 Backbone.js REST 调用

Google Maps API v3 infowindow 关闭事件/回调?

jQuery JSON到字符串?

javascript:检测滚动结束

如何在 JavaScript 或 jQuery 中规范化 HTML?

所有选中复选框的 jQuery 数组(按类)

jQuery DataTables:延迟搜索直到输入 3 个字符或单击按钮

DataTables:无法读取未定义的属性长度

Rails 5:如何将 $(document).ready() 与 turbo-links 一起使用

JQuery - 按值查找单选按钮

在ajax请求中检测重定向?

如何在没有鼠标事件的情况下在 jQuery 中获取鼠标位置?

CORS - 如何预检httprequest?

catch forEach 最后一次迭代