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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<template>
  <view
    class="tn-subsection-class tn-subsection"
    :class="[subsectionBackgroundColorClass]"
    :style="[subsectionStyle]"
  >
    <!-- 滑块 -->
    <block v-for="(item, index) in listInfo" :key="index">
      <view
        class="tn-subsection__item tn-text-ellipsis"
        :class="[
          'section-item-' + index,
          noBorderRight(index)
        ]"
        :style="[itemStyle(index)]"
        @tap="click(index)"
      >
        <view class="tn-subsection__item--text tn-text-ellipsis" :style="[textStyle(index)]">
          {{ item.name }}
        </view>
      </view>
    </block>
    <!-- 背景 -->
    <view
      class="tn-subsection__bg"
      :class="[itemBarClass]"
      :style="[itemBarStyle]"
    ></view>
  </view>
</template>
 
<script>
  import componentsColorMixin from '../../libs/mixin/components_color.js'
  export default {
    mixins: [componentsColorMixin],
    name: 'tn-subsection',
    props: {
      // 模式选择
      // button 按钮模式 subsection 分段模式
      mode: {
        type: String,
        default: 'subsection'
      },
      // 组件高度
      height: {
        type: Number,
        default: 60
      },
      // tab的数据
      list: {
        type: Array,
        default() {
          return []
        }
      },
      // 当前活动tab的index
      current: {
        type: [Number, String],
        default: 0
      },
      // 激活时的字体颜色
      activeColor: {
        type: String,
        default: '#FFFFFF'
      },
      // 未激活时的字体颜色
      inactiveColor: {
        type: String,
        default: '#AAAAAA'
      },
      // 激活tab的字体是否加粗
      bold: {
        type: Boolean,
        default: false
      },
      backgroundColor: {
        type: String,
        default: '#F4F4F4'
      },
      // 滑块的颜色
      buttonColor: {
        type: String,
        default: '#01BEFF'
      },
      // 当mode为button时生效,圆角的值,单位rpx
      borderRadius: {
        type: Number,
        default: 10
      },
      // 是否开启动画
      animation: {
        type: Boolean,
        default: true
      },
      // 动画类型
      // cubic-bezier -> 贝塞尔曲线
      animationType: {
        type: String,
        default: ''
      },
      // 滑动滑块的是否,是否触发震动
      vibrateShort: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 列表数据
        listInfo: [],
        // 子元素的背景样式
        itemBgStyle: {
          width: 0,
          left: 0,
          backgroundColor: '#ffffff',
          height: '100%'
        },
        // 当前选中的滑块
        currentIndex: this.current,
        buttonPadding: 3,
        // 组件初始化的是否current变换不应该震动
        firstVibrateShort: true
      }
    },
    watch: {
      current: {
        handler(val) {
          this.currentIndex = val
          this.changeSectionStatus(val)
        },
        immediate: true
      }
    },
    created() {
      // 将list的数据,传入listInfo数组
      // 接受直接数组形式,或者数组元素为对象的形式,如:['开启', '关闭'],或者[{name: '开启'}, {name: '关闭'}]
      this.listInfo = this.list.map((item, index) => {
        if (typeof item !== 'object') {
          let obj = {
            width: 0,
            name: item
          }
          return obj
        } else {
          item.width = 0
          return obj
        }
      })
    },
    computed: {
      // 设置mode=subsection时,滑块没有样式
      noBorderRight() {
        return index => {
          if (this.mode !== 'subsection') return
          let clazz = ''
          // 不显示右边的边距
          if (index < this.list.length - 1) clazz += ' tn-subsection__item--none-border-right'
          // 显示整个组件的左右边圆角
          if (index === 0) clazz += ' tn-subsection__item--first'
          if (index === this.list.length - 1) clazz += ' tn-subsection__item--last'
          return clazz
        }
      },
      // 文字的样式
      textStyle() {
        return index => {
          let style = {}
          // 设置字体颜色
          if (index === this.currentIndex) {
            style.color = this.activeColor
          } else {
            style.color = this.inactiveColor
          }
          // 字体加粗
          if (index === this.currentIndex && this.bold) style.fontWeight = 'bold'
          // 文字大小
          style.fontSize = (this.fontSize || 26) + this.fontUnit
          return style
        }
      },
      // 每个分段器item的样式
      itemStyle() {
        return index => {
          let style = {}
          if (this.fontSizeStyle) {
            style.fontSize = this.fontSizeStyle
          }
          if (this.mode === 'subsection') {
            // 设置border的样式
            style.borderColor = this.buttonColor
            style.borderWidth = '1rpx'
            style.borderStyle = 'solid'
          }
          return style
        }
      },
      // mode = button时,设置外层view的样式
      subsectionStyle() {
        let style = {}
        style.height = this.height + 'rpx'
        if (this.mode === 'button') {
          style.backgroundColor = this.backgroundColorStyle
          style.padding = `${this.buttonPadding}px`
          style.borderRadius = `${this.borderRadius}rpx`
        }
        return style
      },
      // mode = button时,设置外层view的背景class
      subsectionBackgroundColorClass() {
        let clazz = ''
        if (this.mode === 'button' && this.backgroundColorClass) {
          clazz = this.backgroundColorClass
        }
        return clazz
      },
      itemBarClass() {
        let clazz = ''
        const buttonBgClass = this.$t.color.getBackgroundColorInternalClass(this.buttonColor)
        if (this.animation) {
          clazz += ' tn-subsection__bg__animation'
          if (this.animationType) {
            clazz += ` tn-subsection__bg__animation--${this.animationType}`
          }
        }
        if (buttonBgClass) {
          clazz += ` ${buttonBgClass}`
        }
        return clazz
      },
      // 滑块样式
      itemBarStyle() {
        let style = {}
        const buttonBgStyle = this.$t.color.getBackgroundColorStyle(this.buttonColor)
        if (buttonBgStyle) {
          style.backgroundColor = this.buttonColor
        }
        style.zIndex = 1
        if (this.mode === 'button') {
          style.borderRadius = `${this.borderRadius}rpx`
          style.bottom = `${this.buttonPadding}px`
          style.height = (this.height - (this.buttonPadding * 4)) + 'rpx'
          style.zIndex = 0
        }
        return Object.assign(this.itemBgStyle, style)
      }
    },
    mounted() {
      // 等待加载组件完成
      setTimeout(() => {
        this.getTabsInfo()
      }, 10)
    },
    methods: {
      // 改变滑块样式
      changeSectionStatus(val) {
        if (this.mode === 'subsection') {
          // 根据滑块在最左和最右时,显示对应的圆角
          if (val === this.list.length - 1) {
            this.itemBgStyle.borderRadius = `0 ${this.buttonPadding}px ${this.buttonPadding}px 0`
          }
          if (val === 0) {
            this.itemBgStyle.borderRadius = `${this.buttonPadding}px 0 0 ${this.buttonPadding}px`
          }
          if (val > 0 && val < this.list.length - 1) {
            this.itemBgStyle.borderRadius = '0'
          }
        }
        // 更新滑块的位置
        setTimeout(() => {
          this.itemBgLeft()
        }, 10)
        if (this.vibrateShort && !this.firstVibrateShort) {
          // 使手机产生短促震动,微信小程序有效,APP(HX 2.6.8)和H5无效
          // #ifndef H5
          uni.vibrateShort();
          // #endif
        }
        this.firstVibrateShort = false
      },
      // 获取各个tab的节点信息
      getTabsInfo() {
        let view = uni.createSelectorQuery().in(this)
        for (let i = 0; i < this.list.length; i++) {
          view.select('.section-item-' + i).boundingClientRect()
        }
        view.exec(res => {
          // 如果没有获取到,则重新获取
          if (!res.length) {
            setTimeout(() => {
              this.getTabsInfo()
              return
            }, 10)
          }
          // 将每个分段器的宽度放入listInfo中
          res.map((item, index) => {
            this.listInfo[index].width = item.width
          })
          // 初始化滑块的宽度
          if (this.mode === 'subsection') {
            this.itemBgStyle.width = this.listInfo[0].width + 'px'
          } else if (this.mode === 'button') {
            this.itemBgStyle.width = this.listInfo[0].width + 'px'
          }
          
          // 初始化滑块的位置
          this.itemBgLeft()
        })
      },
      // 设置滑块的位置
      itemBgLeft() {
        let left = 0
        // 计算当前活跃item到组件左边的距离
        this.listInfo.map((item, index) => {
          if (index < this.currentIndex) left += item.width
        })
        // 根据不同的模式,计算滑块的位置
        if (this.mode === 'subsection') {
          this.itemBgStyle.left = left + 'px'
        } else if (this.mode === 'button') {
          this.itemBgStyle.left = left + this.buttonPadding + 'px'
        }
      },
      
      // 点击事件
      click(index) {
        // 不允许点击当前激活的选项
        if (index === this.currentIndex) return
        this.currentIndex = index
        this.changeSectionStatus(index)
        this.$emit('change', {
          index: Number(index),
          name: this.listInfo[index]['name']
        })
      }
    }
  }
</script>
 
<style lang="scss" scoped>
  
  .tn-subsection {
    /* #ifndef APP-PLUS */
    display: flex;
    flex-direction: row;
    /* #endif */
    align-items: center;
    overflow: hidden;
    position: relative;
    
    &__item {
      /* #ifndef APP-PLUS */
      display: flex;
      flex-direction: row;
      /* #endif */
      flex: 1;
      text-align: center;
      font-size: 26rpx;
      height: 100%;
      align-items: center;
      justify-content: center;
      color: #FFFFFF;
      padding: 0 6rpx;
      
      
      &--text {
        transition: all 0.3s;
        color: #FFFFFF;
        /* #ifndef APP-PLUS */
        display: flex;
        flex-direction: row;
        /* #endif */
        align-items: center;
        position: relative;
        z-index: 3;
      }
      
      &--first {
        border-top-left-radius: 8rpx;
        border-bottom-left-radius: 8rpx;
      }
      
      &--last {
        border-top-right-radius: 8rpx;
        border-bottom-right-radius: 8rpx;
      }
      
      &--none-border-right {
        border-right: none !important;
      }
    }
    
    &__bg {
      background-color: $tn-main-color;
      position: absolute;
      z-index: -1;
      transition-property: all;
      transition-duration: 0s;
      transition-timing-function: linear;
      
      &__animation {
        transition-duration: 0.25s !important;
        
        &--cubic-bezier {
          transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55) !important;
        }
      }
    }
  }
  
</style>