Skip to content

Commit 6091566

Browse files
fix: 🐛 修复微信小程序在iOS设备上处于后台一段时间后抖动的问题
Closes: #694
1 parent a90f4ad commit 6091566

2 files changed

Lines changed: 42 additions & 41 deletions

File tree

src/uni_modules/wot-design-uni/components/wd-swiper-nav/wd-swiper-nav.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
>
1111
<block v-if="type === 'dots' || type === 'dots-bar'">
1212
<view
13-
v-for="(item, index) in total"
13+
v-for="(_, index) in total"
1414
:key="index"
1515
:class="`wd-swiper-nav__item--${type} ${current === index ? 'is-active' : ''} is-${direction}`"
1616
></view>

src/uni_modules/wot-design-uni/components/wd-swiper/wd-swiper.vue

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<image
2424
v-if="isImage(item)"
2525
:src="isObj(item) ? item[valueKey] : item"
26-
:class="`wd-swiper__image ${customImageClass} ${customItemClass} ${getCustomItemClass(navCurrent, index, list)}`"
26+
:class="`wd-swiper__image ${customImageClass} ${customItemClass} ${getCustomItemClass(currentValue, index, list)}`"
2727
:style="{ height: addUnit(height) }"
2828
:mode="imageMode"
2929
/>
@@ -33,7 +33,7 @@
3333
:style="{ height: addUnit(height) }"
3434
:src="isObj(item) ? item[valueKey] : item"
3535
:poster="isObj(item) ? item.poster : ''"
36-
:class="`wd-swiper__video ${customItemClass} ${getCustomItemClass(navCurrent, index, list)}`"
36+
:class="`wd-swiper__video ${customItemClass} ${getCustomItemClass(currentValue, index, list)}`"
3737
@play="handleVideoPaly"
3838
@pause="handleVideoPause"
3939
:enable-progress-gesture="false"
@@ -47,7 +47,7 @@
4747
</swiper>
4848

4949
<template v-if="indicator">
50-
<slot name="indicator" :current="navCurrent" :total="list.length"></slot>
50+
<slot name="indicator" :current="currentValue" :total="list.length"></slot>
5151
<wd-swiper-nav
5252
v-if="!$slots.indicator"
5353
:custom-class="customIndicatorClass"
@@ -83,7 +83,21 @@ import type { SwiperNavProps } from '../wd-swiper-nav/types'
8383
8484
const props = defineProps(swiperProps)
8585
const emit = defineEmits(['click', 'change', 'animationfinish', 'update:current'])
86-
const navCurrent = ref<number>(0) // 当前滑块
86+
const navCurrent = ref<number>(props.current) // 当前滑块 swiper使用
87+
const currentValue = ref<number>(props.current) // 当前滑块
88+
89+
/**
90+
* 更新当前滑块
91+
* @param current 当前滑块索引
92+
* @param force 是否强制更新swiper绑定的的current
93+
*/
94+
const updateCurrent = (current: number, force: boolean = false) => {
95+
currentValue.value = current
96+
if (force) {
97+
navCurrent.value = current
98+
}
99+
emit('update:current', current)
100+
}
87101
88102
const videoPlaying = ref<boolean>(false) // 当前是否在播放视频
89103
@@ -99,17 +113,15 @@ watch(
99113
} else if (val >= props.list.length) {
100114
props.loop ? goToStart() : goToEnd()
101115
} else {
102-
go(val)
116+
navTo(val)
103117
}
104-
emit('update:current', navCurrent.value)
105-
},
106-
{ immediate: true }
118+
}
107119
)
108120
109121
const swiperIndicator = computed(() => {
110122
const { list, direction, indicatorPosition, indicator } = props
111123
const swiperIndicator: Partial<SwiperNavProps> = {
112-
current: navCurrent.value || 0,
124+
current: currentValue.value || 0,
113125
total: list.length || 0,
114126
direction: direction || 'horizontal',
115127
indicatorPosition: indicatorPosition || 'bottom'
@@ -138,16 +150,17 @@ const isImage = (item: string | SwiperList) => {
138150
return getMediaType(item, 'image')
139151
}
140152
141-
function go(index: number) {
142-
navCurrent.value = index
153+
function navTo(index: number) {
154+
if (index === currentValue.value) return
155+
updateCurrent(index, true)
143156
}
144157
145158
function goToStart() {
146-
navCurrent.value = 0
159+
navTo(0)
147160
}
148161
149162
function goToEnd() {
150-
navCurrent.value = props.list.length - 1
163+
navTo(props.list.length - 1)
151164
}
152165
153166
// 视频播放
@@ -193,16 +206,15 @@ function getCustomItemClass(current: number, index: number, list: string[] | Swi
193206
194207
/**
195208
* 轮播滑块切换时触发
196-
* @param e
197209
*/
198210
function handleChange(e: { detail: { current: number; source: string } }) {
199211
const { current, source } = e.detail
200-
const previous = navCurrent.value
201-
navCurrent.value = current
202-
// #ifndef MP-WEIXIN
203-
emit('update:current', navCurrent.value)
204-
// #endif
212+
const previous = currentValue.value
205213
emit('change', { current, source })
214+
if (current !== currentValue.value) {
215+
const forceUpdate = source === 'autoplay' || source === 'touch'
216+
updateCurrent(current, forceUpdate)
217+
}
206218
handleVideoChange(previous, current)
207219
}
208220
@@ -249,11 +261,10 @@ function handleStopVideoPaly(index: number) {
249261
*/
250262
function handleAnimationfinish(e: { detail: { current: any; source: string } }) {
251263
const { current, source } = e.detail
252-
// #ifdef MP-WEIXIN
253-
// 兼容微信swiper抖动的问题
254-
emit('update:current', navCurrent.value)
255-
// #endif
256-
264+
if (current !== currentValue.value) {
265+
const forceUpdate = source === 'autoplay' || source === 'touch'
266+
updateCurrent(current, forceUpdate)
267+
}
257268
/**
258269
* 滑块动画结束时触发
259270
*/
@@ -269,27 +280,17 @@ function handleClick(index: number, item: string | SwiperList) {
269280
emit('click', { index, item })
270281
}
271282
272-
function handleIndicatorChange(e: { dir: any; source: string }) {
273-
const { dir, source } = e
274-
doIndicatorBtnChange(dir, source)
275-
}
276-
277-
function doIndicatorBtnChange(dir: string, source: string) {
283+
function handleIndicatorChange({ dir }: { dir: 'prev' | 'next' }) {
278284
const { list, loop } = props
279285
const total = list.length
280-
let nextPos = dir === 'next' ? navCurrent.value + 1 : navCurrent.value - 1
281-
286+
let nextPos = dir === 'next' ? currentValue.value + 1 : currentValue.value - 1
282287
if (loop) {
283-
nextPos = dir === 'next' ? (navCurrent.value + 1) % total : (navCurrent.value - 1 + total) % total
288+
nextPos = dir === 'next' ? (currentValue.value + 1) % total : (currentValue.value - 1 + total) % total
284289
} else {
285-
nextPos = nextPos < 0 || nextPos >= total ? navCurrent.value : nextPos
290+
nextPos = nextPos < 0 || nextPos >= total ? currentValue.value : nextPos
286291
}
287-
288-
if (nextPos === navCurrent.value) return
289-
290-
navCurrent.value = nextPos
291-
emit('change', { current: nextPos, source })
292-
emit('update:current', navCurrent.value)
292+
if (nextPos === currentValue.value) return
293+
navTo(nextPos)
293294
}
294295
</script>
295296

0 commit comments

Comments
 (0)