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