fix: 优化长图展示

- 在图片未完全展示完成但是高度已经确定的时候开始跳转
This commit is contained in:
huangze
2025-07-04 11:01:26 +08:00
parent cd1c870b45
commit f044f32195

View File

@@ -44,15 +44,15 @@ export default {
} }
}, },
watch: { watch: {
// 监听路由参数变化 // // 监听路由参数变化
'$route.params': { // '$route.params': {
handler(newParams) { // handler(newParams) {
if (newParams.position) { // if (newParams.position) {
this.scrollToBlock(); // this.scrollToBlock();
} // }
}, // },
immediate: true // immediate: true
}, // },
// 监听当前块position变化 // 监听当前块position变化
'currentBlockId': function (newId) { 'currentBlockId': function (newId) {
if (newId) { if (newId) {
@@ -74,16 +74,16 @@ export default {
// 使用setTimeout确保 DOM 已经完全渲染 // 使用setTimeout确保 DOM 已经完全渲染
this.$nextTick(() => { this.$nextTick(() => {
setTimeout(() => { // setTimeout(() => {
const element = document.getElementById(`block-${position}`); const element = document.getElementById(`block-${position}`);
if (element) { if (element) {
// 滚动到元素位置 // 滚动到元素位置
window.scrollTo({ window.scrollTo({
top: element.offsetTop - 70, // 偏移70px给顶部留出空间 top: element.offsetTop - 70, // 偏移70px给顶部留出空间
}); });
this.hasScrolled = true; this.hasScrolled = true;
} }
}, 800); // 添加短暂延迟确保DOM已更新 // }, 800); // 添加短暂延迟确保DOM已更新
}); });
} }
}, },
@@ -98,10 +98,27 @@ export default {
position: position position: position
} }
}) })
},
handleImageLoad() {
const images = document.querySelectorAll('.block-image');
const loadPromises = Array.from(images).map(img => {
return new Promise(resolve => {
const checkInterval = setInterval(() => {
if (img.naturalHeight > 0) {
clearInterval(checkInterval);
resolve();
}
},50);
});
});
Promise.all(loadPromises).then(() => {
this.scrollToBlock()
});
} }
}, }, mounted() {
mounted() { this.handleImageLoad()
this.scrollToBlock();
} }
} }
</script> </script>