修复top=0的组件无法吸附问题

This commit is contained in:
icssoa
2025-11-13 22:34:34 +08:00
parent 629652c500
commit 19b7435421

View File

@@ -105,32 +105,37 @@ const stickyTop = computed(() => {
// 获取sticky元素的位置信息并更新rect // 获取sticky元素的位置信息并更新rect
function getRect() { function getRect() {
nextTick(() => { const next = () => {
setTimeout( uni.createSelectorQuery()
() => { .in(proxy)
uni.createSelectorQuery() .select(".cl-sticky")
.in(proxy) .boundingClientRect()
.select(".cl-sticky") .exec((nodes) => {
.boundingClientRect() if (isEmpty(nodes)) {
.exec((nodes) => { return;
if (isEmpty(nodes)) { }
return;
}
const node = nodes[0] as NodeInfo; const node = nodes[0] as NodeInfo;
// 赋值时做空值处理,保证类型安全 // 赋值时做空值处理,保证类型安全
rect.height = node.height ?? 0; rect.height = node.height ?? 0;
rect.width = node.width ?? 0; rect.width = node.width ?? 0;
rect.left = node.left ?? 0; rect.left = node.left ?? 0;
// top需要减去offsetTop并加上当前滚动距离保证吸顶准确 // top需要减去offsetTop并加上当前滚动距离保证吸顶准确
rect.top = (node.top ?? 0) - props.offsetTop + scrollTop.value; rect.top = (node.top ?? 0) - props.offsetTop + scrollTop.value;
}); });
}, };
isHarmony() ? 300 : 0
); if (isHarmony()) {
}); nextTick(() => {
setTimeout(() => {
next();
}, 300);
});
} else {
next();
}
} }
onMounted(() => { onMounted(() => {