jQuery 中的 jQuery.position() 和 jQuery.offset() 有什么区别?
jQueryposition()方法
该position()方法获取元素相对于其偏移父元素的顶部和左侧位置。
返回的对象包含两个Integer属性,top和left。为了准确计算,请确保对边距、边框和填充使用像素值。此方法仅适用于可见元素。
示例
您可以尝试运行以下代码以了解如何使用position()jQuery中的方法:
The jQuery Example Click on any square:
jQueryoffset()方法
该offset()方法获取第一个匹配元素相对于文档的当前偏移量(以像素为单位)。
示例
您可以尝试运行以下代码来学习如何使用offset()jQuery中的方法:
jQuery position() methodjQuery offset() method Click on any square: