dengjunjie
5 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<template>
  <view
    class="tn-stack-swiper-class tn-stack-swiper"
    :style="{
      width: $t.string.getLengthUnitValue(width),
      height: $t.string.getLengthUnitValue(height)
    }"
    :list="swiperList"
    :itemData="itemData"
    :currentIndex="swiperIndex"
    :autoplayFlag="autoplayFlag"
    :change:list="wxs.listObserver"
    :change:itemData="wxs.itemDataObserver"
    :change:currentIndex="wxs.swiperIndexChange"
    :change:autoplayFlag="wxs.autoplayFlagChange"
  >
    <block v-for="(item, index) in list" :key="index">
      <!-- #ifdef MP-WEIXIN -->
      <view
        class="tn-stack-swiper__item tn-stack-swiper__item__transition"
        :class="[`tn-stack-swiper__item--${direction}`]"
        :data-index="index"
        :data-switchRate="switchRate"
        @touchstart="wxs.touchStart"
        :catch:touchmove="touching?wxs.touchMove:''"
        :catch:touchend="touching?wxs.touchEnd:''"
      >
        <image class="tn-stack-swiper__image" :src="item.image"></image>
      </view>
      <!-- #endif -->
      
      <!-- #ifndef MP-WEIXIN -->
      <view
        class="tn-stack-swiper__item"
        :class="[`tn-stack-swiper__item--${direction}`]"
        :data-index="index"
        :data-switchRate="switchRate"
        @touchstart="wxs.touchStart"
        @touchmove="wxs.touchMove"
        @touchend="wxs.touchEnd"
      >
        <image class="tn-stack-swiper__image" :src="item.image"></image>
      </view>
      <!-- #endif -->
    </block>
  </view>
</template>
 
<!-- #ifdef MP-WEIXIN -->
<script src="./index.wxs" lang="wxs" module="wxs"></script>
<!-- #endif -->
<!-- #ifndef MP-WEIXIN -->
<script src="./index-h5.wxs" lang="wxs" module="wxs"></script>
<!-- #endif -->
<script>
  export default {
    name: 'tn-stack-swiper',
    props: {
      // 显示图片的列表数据
      // {
      //   // 图片地址
      //   image: 'xxx'
      // }
      list: {
        type: Array,
        default() {
          return []
        }
      },
      // 轮播容器的宽度 rpx
      width: {
        type: [String, Number],
        default: '100%'
      },
      // 轮播容器的高度 rpx
      height: {
        type: [String, Number],
        default: 500
      },
      // 自动切换
      autoplay: {
        type: Boolean,
        default: false
      },
      // 自动切换时长 ms
      interval: {
        type: Number,
        default: 5000
      },
      // 滑动切换移动比例, [0 - 100]
      // 比例相对于item的宽度
      switchRate: {
        type: Number,
        default: 30
      },
      // 缩放比例 [0-1]
      scaleRate: {
        type: Number,
        default: 0.1
      },
      // 下一轮播偏移比例
      translateRate: {
        type: Number,
        default: 16
      },
      // 下一轮播透明比例
      opacityRate: {
        type: Number,
        default:10
      },
      // 滑动方向
      // horizontal -> 水平 vertical -> 垂直
      direction: {
        type: String,
        default: 'horizontal'
      }
    },
    data() {
      return {
        autoplayTimer: null,
        // window窗口的宽度
        windowWidth: 0,
        // 轮播item的宽度
        swiperItemWidth: 0,
        // 轮播item的高度
        swiperItemHeight: 0,
        // 当前选中的轮播item
        swiperIndex: -1,
        // 标记是否开始触摸
        touching: true,
        // 轮播列表信息
        swiperList: [],
        // 标记当前是否为自动播放
        autoplayFlag: false
      }
    },
    computed: {
      itemData() {
        return {
          windowWidth: this.windowWidth,
          itemWidth: this.swiperItemWidth,
          itemHeight: this.swiperItemHeight,
          direction: this.direction,
          autoplaying: this.autoplayFlag
        }
      }
    },
    watch: {
      list(val) {
        this.swiperList = []
        this.$nextTick(() => {
          this.initSwiperRectInfo()
        })
      },
      autoplay(val) {
        if (!val) {
          this.clearAutoPlayTimer()
        } else {
          this.setAutoPlay()
        }
      }
    },
    created() {
      this.autoplayFlag = this.autoplay
    },
    mounted() {
      this.$nextTick(() => {
        this.initSwiperRectInfo()
      })
    },
    destroyed() {
      this.clearAutoPlayTimer()
    },
    methods: {
      // 初始化轮播容器信息
      async initSwiperRectInfo() {
        // 用于一开始绑定事件
        // this.touching = true
        // 获取轮播item的宽度
        const swiperItemRect = await this._tGetRect('.tn-stack-swiper__item')
        if (!swiperItemRect || !swiperItemRect.width || !swiperItemRect.height) {
          setTimeout(() => {
            this.initSwiperRectInfo()
          }, 50)
          return
        }
        this.swiperItemWidth = swiperItemRect.width
        this.swiperItemHeight = swiperItemRect.height
        // this.touching = false
        
        // 获取系统的窗口宽度信息
        const systemInfo = uni.getSystemInfoSync()
        this.windowWidth = systemInfo.windowWidth
        this.swiperIndex = 0
        
        // 设置对应swiper元素的位置和层级信息
        this.swiperList = this.list.map((item, index) => {
          
          const scale = 1 - (this.scaleRate * index)
          
          if (this.direction === 'horizontal') {
            item.translateX = ((index * this.translateRate) * 0.01 * this.swiperItemWidth)
          } else if (this.direction === 'vertical') {
            item.translateY = ((index * this.translateRate) * 0.01 * this.swiperItemHeight)
          }
          item.opacity = (1 - ((index * this.opacityRate) * 0.01))
          item.zIndex = this.list.length - index
          item.scale = scale <= 0 ? 0 : scale
          
          return item
        })
        
        this.setAutoPlay()
      },
      // 设置自动切换轮播
      setAutoPlay() {
        if (this.autoplay) {
          this.clearAutoPlayTimer()
          this.autoplayFlag = true
          this.autoplayTimer = setInterval(() => {
            this.swiperIndex = this.swiperIndex + 1 > this.swiperList.length - 1 ? 0 : this.swiperIndex + 1
          }, this.interval)
        }
      },
      // 清除自动切换定时器
      clearAutoPlayTimer() {
        if (this.autoplayTimer != null) {
          this.autoplayFlag = false
          clearInterval(this.autoplayTimer)
        }
      },
      // 修改轮播选中index
      changeSwiperIndex(e) {
        // console.log(e.index);
        this.swiperIndex = e.index
        this.$emit('change', { index: e.index })
      },
      // 修改触摸状态
      changeTouchState(e) {
        this.touching = e.touching
      },
      // 打印日志
      printLog(data) {
        console.log("log", data);
      }
    }
  }
</script>
 
<style lang="scss" scoped>
  .tn-stack-swiper {
    position: relative;
    
    &__item {
      position: absolute;
      border-radius: 20rpx;
      overflow: hidden;
      
      &--horizontal {
        width: 88%;
        height: 100%;
        transform-origin: left center;
      }
      
      &--vertical {
        width: 100%;
        height: 88%;
        transform-origin: top center;
      }
      
      &__transition {
        transition-property: transform,opacity;
        transition-duration: 0.25s;
        transition-timing-function: ease-out;
        // transition: transform, opacity 0.25s ease-in-out !important;
      }
    }
    
    &__image {
      width: 100%;
      height: 100%;
    }
  }
</style>