|
2 | 2 | <view class="u-swiper-wrap" :style="{
|
3 | 3 | borderRadius: `${borderRadius}rpx`
|
4 | 4 | }">
|
5 |
| - <swiper @change="change" @animationfinish="animationfinish" :interval="interval" :circular="circular" :duration="duration" :autoplay="autoplay" |
| 5 | + <swiper :current="elCurrent" @change="change" @animationfinish="animationfinish" :interval="interval" :circular="circular" :duration="duration" :autoplay="autoplay" |
6 | 6 | :previous-margin="effect3d ? effect3dPreviousMargin + 'rpx' : '0'" :next-margin="effect3d ? effect3dPreviousMargin + 'rpx' : '0'"
|
7 | 7 | :style="{
|
8 | 8 | height: height + 'rpx'
|
9 | 9 | }">
|
10 | 10 | <swiper-item class="u-swiper-item" v-for="(item, index) in list" :key="index">
|
11 |
| - <view class="u-list-image-wrap" @tap.stop.prevent="listClick(index)" :class="[current != index ? 'u-list-scale' : '']" :style="{ |
| 11 | + <view class="u-list-image-wrap" @tap.stop.prevent="listClick(index)" :class="[uCurrent != index ? 'u-list-scale' : '']" :style="{ |
12 | 12 | borderRadius: `${borderRadius}rpx`,
|
13 |
| - transform: effect3d && current != index ? 'scaleY(0.9)' : 'scaleY(1)', |
14 |
| - margin: effect3d && current != index ? '0 20rpx' : 0, |
| 13 | + transform: effect3d && uCurrent != index ? 'scaleY(0.9)' : 'scaleY(1)', |
| 14 | + margin: effect3d && uCurrent != index ? '0 20rpx' : 0, |
15 | 15 | backgroundColor: bgColor
|
16 | 16 | }">
|
17 | 17 | <image class="u-swiper-image" :src="item[name]" :mode="imgMode"></image>
|
|
30 | 30 | padding: `0 ${effect3d ? '74rpx' : '24rpx'}`
|
31 | 31 | }">
|
32 | 32 | <block v-if="mode == 'rect'">
|
33 |
| - <view class="u-indicator-item-rect" :class="{ 'u-indicator-item-rect-active': index == current }" v-for="(item, index) in list" |
| 33 | + <view class="u-indicator-item-rect" :class="{ 'u-indicator-item-rect-active': index == uCurrent }" v-for="(item, index) in list" |
34 | 34 | :key="index"></view>
|
35 | 35 | </block>
|
36 | 36 | <block v-if="mode == 'dot'">
|
37 |
| - <view class="u-indicator-item-dot" :class="{ 'u-indicator-item-dot-active': index == current }" v-for="(item, index) in list" |
| 37 | + <view class="u-indicator-item-dot" :class="{ 'u-indicator-item-dot-active': index == uCurrent }" v-for="(item, index) in list" |
38 | 38 | :key="index"></view>
|
39 | 39 | </block>
|
40 | 40 | <block v-if="mode == 'round'">
|
41 |
| - <view class="u-indicator-item-round" :class="{ 'u-indicator-item-round-active': index == current }" v-for="(item, index) in list" |
| 41 | + <view class="u-indicator-item-round" :class="{ 'u-indicator-item-round-active': index == uCurrent }" v-for="(item, index) in list" |
42 | 42 | :key="index"></view>
|
43 | 43 | </block>
|
44 | 44 | <block v-if="mode == 'number'">
|
45 |
| - <view class="u-indicator-item-number">{{ current + 1 }}/{{ list.length }}</view> |
| 45 | + <view class="u-indicator-item-number">{{ uCurrent + 1 }}/{{ list.length }}</view> |
46 | 46 | </block>
|
47 | 47 | </view>
|
48 | 48 | </view>
|
|
156 | 156 | bgColor: {
|
157 | 157 | type: String,
|
158 | 158 | default: '#f3f4f6'
|
| 159 | + }, |
| 160 | + // 初始化时,默认显示第几项 |
| 161 | + current: { |
| 162 | + type: [Number, String], |
| 163 | + default: 0 |
159 | 164 | }
|
160 | 165 | },
|
161 | 166 | watch: {
|
162 |
| - // 如果外部的list发生变化,判断长度是否被修改,如果前后长度不一致,重置current值,避免溢出 |
| 167 | + // 如果外部的list发生变化,判断长度是否被修改,如果前后长度不一致,重置uCurrent值,避免溢出 |
163 | 168 | list(nVal, oVal) {
|
164 |
| - if(nVal.length !== oVal.length) this.current = 0; |
| 169 | + if(nVal.length !== oVal.length) this.uCurrent = 0; |
| 170 | + }, |
| 171 | + // 监听外部current的变化,实时修改内部依赖于此测uCurrent值,如果更新了current,而不是更新uCurrent, |
| 172 | + // 就会错乱,因为指示器是依赖于uCurrent的 |
| 173 | + current(n) { |
| 174 | + this.uCurrent = n; |
165 | 175 | }
|
166 | 176 | },
|
167 | 177 | data() {
|
168 | 178 | return {
|
169 |
| - current: 0 // 当前活跃的swiper-item的index |
| 179 | + uCurrent: this.current // 当前活跃的swiper-item的index |
170 | 180 | };
|
171 | 181 | },
|
172 | 182 | computed: {
|
|
186 | 196 | tmp = '12rpx';
|
187 | 197 | }
|
188 | 198 | return tmp;
|
| 199 | + }, |
| 200 | + // 因为uni的swiper组件的current参数只接受Number类型,这里做一个转换 |
| 201 | + elCurrent() { |
| 202 | + return Number(this.current); |
189 | 203 | }
|
190 | 204 | },
|
191 | 205 | methods: {
|
|
194 | 208 | },
|
195 | 209 | change(e) {
|
196 | 210 | let current = e.detail.current;
|
197 |
| - this.current = current; |
| 211 | + this.uCurrent = current; |
198 | 212 | // 发出change事件,表示当前自动切换的index,从0开始
|
199 | 213 | this.$emit('change', current);
|
200 | 214 | },
|
201 | 215 | // 头条小程序不支持animationfinish事件,改由change事件
|
202 |
| - // 暂不监听此事件,因为不再给swiper绑定current属性 |
| 216 | + // 暂不监听此事件,因为不再给swiper绑定uCurrent属性 |
203 | 217 | animationfinish(e) {
|
204 | 218 | // #ifndef MP-TOUTIAO
|
205 |
| - // this.current = e.detail.current; |
| 219 | + // this.uCurrent = e.detail.current; |
206 | 220 | // #endif
|
207 | 221 | }
|
208 | 222 | }
|
|
0 commit comments