$(() => {
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()
最新推荐文章于 2023-04-09 10:05:41 发布
本文深入探讨了JQuery中offset()与position()方法的区别,通过示例代码展示了这两种方法的具体应用,并给出了它们的原生JavaScript实现方式。

237

被折叠的 条评论
为什么被折叠?



