$(() => {
console.log($('p').offset())
console.log($('p').position())
console.log(offset($('p')[0]))
console.log(position($('p')[0]))
// jquery offset原生实现
function offset(target) {
var top = 0,
left = 0
while(target.offsetParent) {
top += target.offsetTop
left += target.offsetLeft
target = target.offsetParent
}
return {
top: top,
left: left,
}
}
// jquery position原生实现
function position(target) {
return {
top: target.offsetTop,
left: target.offsetLeft,
}
}
})
js原生实现jquery方法offset()和position()
最新推荐文章于 2025-03-30 13:57:44 发布