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 />
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"
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
8484const props = defineProps (swiperProps )
8585const 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
88102const 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
109121const 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
145158function goToStart() {
146- navCurrent . value = 0
159+ navTo ( 0 )
147160}
148161
149162function 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 */
198210function 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 */
250262function 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