修复bug

This commit is contained in:
2025-12-17 13:23:08 +08:00
parent d9cb736597
commit bfd0676196
2 changed files with 21 additions and 13 deletions

View File

@@ -151,6 +151,7 @@ let accumulatedTime = 0
let rafId = null
let controlsTimer = null
let isSwitching = false
const loadedImageUrls = new Set() // 记录已加载的图片 URL避免重复加载检测
const currentPhoto = computed(() => props.photos[currentIndex.value])
// 加载期间给个默认背景或黑屏,避免报错
@@ -205,6 +206,7 @@ const preloadAllImages = async () => {
const promises = props.photos.map(photo => {
const url = getImgUrl(photo.original || photo.compressed)
return loadImage(url).then(() => {
loadedImageUrls.add(url) // 记录已加载的 URL
loadedCount.value++
loadingProgress.value = Math.floor((loadedCount.value / totalCount.value) * 100)
})
@@ -251,15 +253,17 @@ const loop = (timestamp) => {
const handleAutoSwitch = async () => {
isSwitching = true
const nextIdx = (currentIndex.value + 1) % props.photos.length
// 即使预加载过,也检查一下(通常会立即返回)
const nextPhoto = props.photos[nextIdx]
const nextUrl = getImgUrl(nextPhoto.original || nextPhoto.compressed)
const bufferTimeout = setTimeout(() => { isBuffering.value = true }, 100)
await loadImage(nextUrl)
clearTimeout(bufferTimeout)
isBuffering.value = false
// 只有未加载过的图片才需要等待加载
if (!loadedImageUrls.has(nextUrl)) {
const bufferTimeout = setTimeout(() => { isBuffering.value = true }, 100)
await loadImage(nextUrl)
loadedImageUrls.add(nextUrl)
clearTimeout(bufferTimeout)
isBuffering.value = false
}
nextSlide(true)
isSwitching = false
@@ -286,13 +290,18 @@ const prevSlide = async () => {
accumulatedTime = 0
progress.value = 0
const prevIdx = (currentIndex.value - 1 + props.photos.length) % props.photos.length
isSwitching = true
isBuffering.value = true
const prevPhoto = props.photos[prevIdx]
await loadImage(getImgUrl(prevPhoto.original || prevPhoto.compressed))
isBuffering.value = false
isSwitching = false
const prevUrl = getImgUrl(prevPhoto.original || prevPhoto.compressed)
// 只有未加载过的图片才需要显示 buffering
if (!loadedImageUrls.has(prevUrl)) {
isSwitching = true
isBuffering.value = true
await loadImage(prevUrl)
loadedImageUrls.add(prevUrl)
isBuffering.value = false
isSwitching = false
}
currentIndex.value = prevIdx
emit('update:index', currentIndex.value)

View File

@@ -81,7 +81,6 @@
<!-- 每个 Slide 的图片 -->
<img
:src="getImgUrl(photo.original || photo.compressed)"
loading="lazy"
/>
</div>
</swiper-slide>