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
<template>
  <view
    v-if="visibleSync"
    class="tn-tips-class tn-tips"
    :class="[tipsClass]"
    :style="[tipsStyle]"
  >
    <view
      class="tn-tips__content"
      :class="[
        backgroundColorClass,
        fontColorClass
      ]"
      :style="{
        backgroundColor: backgroundColorStyle,
        color: fontColorStyle
      }"
    >{{ msg }}</view>
  </view>
</template>
 
<script>
  
  export default {
    name: 'tn-tips',
    props: {
      // 层级
      zIndex: {
        type: Number,
        default: 0
      },
      // 提示框显示位置 top center bottom
      position: {
        type: String,
        default: 'top'
      },
      // 当位置设置为top的时候,设置距离顶部的距离
      top: {
        type: Number,
        default: 0
      }
    },
    computed: {
      tipsClass() {
        let clazz = ''
        switch (this.position) {
          case 'top':
            clazz += ' tn-tips--top'
            break
          case 'center':
            clazz += ' tn-tips--center'
            break
          case 'bottom':
            clazz += ' tn-tips--bottom'
            break
          default:
            clazz += ' tn-tips--top'
        }
        if (this.showTips) {
          clazz += ' tn-tips--show'
        }
        
        return clazz
      },
      tipsStyle() {
        let style = {}
        if ((this.position === 'top' || this.position === '') && this.top) {
          style.top = this.top + 'px'
        }
        style.zIndex = (this.zIndex ? this.zIndex : this.$t.zIndex.tips) + 1
        
        return style
      },
      backgroundColorStyle() {
        return this.$t.color.getBackgroundColorStyle(this.backgroundColor)
      },
      backgroundColorClass() {
        return this.$t.color.getBackgroundColorInternalClass(this.backgroundColor)
      },
      fontColorStyle() {
        return this.$t.color.getFontColorStyle(this.fontColor)
      },
      fontColorClass() {
        return this.$t.color.getFontColorInternalClass(this.fontColor)
      },
    },
    data() {
      return {
        //关闭提示框定时器
        timer: null,
        // 是否渲染组件
        visibleSync: false,
        // 是否显示内容
        showTips: false,
        // 提示信息
        msg: '',
        // 背景颜色
        backgroundColor: '',
        // 字体颜色
        fontColor: ''
      }
    },
    methods: {
      show(options = {}) {
        const {
          duration = 2000,
          msg = '',
          backgroundColor = '',
          fontColor = ''
        } = options
        
        if (this.timer !== null) clearTimeout(this.timer)
        
        // 如果没有设置内容则不弹出
        if (!msg) {
          this._clearOptions()
          this.$emit('close')
          return
        }
        
        this.msg = msg
        this.backgroundColor = backgroundColor || '#01BEFF'
        this.fontColor = fontColor || '#FFFFFF'
        
        this.change('visibleSync', 'showTips', true)
        
        this.timer = setTimeout(() => {
          clearTimeout(this.timer)
          this.timer = null
          this.change('showTips', 'visibleSync', false)
        }, duration)
      },
      
      // 关闭时先通过动画隐藏弹窗和遮罩,再移除整个组件
      // 打开时,先渲染组件,延时一定时间再让遮罩和弹窗的动画起作用
      change(param1, param2, status) {
        this[param1] = status
        if (status) {
          // #ifdef H5 || MP
          this.timer = setTimeout(() => {
            this[param2] = status
            this.$emit(status ? 'open' : 'close')
          }, 50)
          // #endif
          // #ifndef H5 || MP
          this.$nextTick(() => {
            this[param2] = status
            this.$emit(status ? 'open' : 'close')
          })
          // #endif
        } else {
          this.timer = setTimeout(() => {
            this[param2] = status
            this.$emit(status ? 'open' : 'close')
            this._clearOptions()
          }, 300)
        }
      },
      
      // 清除传递的参数
      _clearOptions() {
        this.msg = ''
        this.backgroundColor = ''
        this.fontColor = ''
      },
    }
  }
</script>
 
<style lang="scss" scoped>
  
  /*注意问题:
   1、fixed 元素宽度无法自适应,所以增加了子元素
   2、fixed 和 display冲突导致动画效果消失,暂时使用visibility替代
  */
  .tn-tips {
    height: auto;
    position: fixed;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease-in-out;
    opacity: 0;
    
    &__content {
      word-wrap: break-word;
      word-break: break-all;
      width: 100%;
      height: auto;
      text-align: center;
      background-color: rgba(0, 0, 0, 0.7);
      color: #FFFFFF;
    }
    
    &--top {
      width: 100% !important;
      /* padding: 18rpx 30rpx; */
      top: 0;
      left: 0;
      transform: translateY(-100%) translateZ(0);
      word-break: break-all;
    }
    
    &--center {
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
    
    &--bottom {
      bottom: 120rpx;
      left: 50%;
      transform: translateX(-50%);
    }
    
    &--center, &--bottom {
      .content {
        border-radius: 8rpx;
        padding: 0;
      }
    }
    
    &--center, &--bottom {
      .tn-tips__content {
        padding: 18rpx 30rpx !important;
      }
    }
    
    &--show {
      opacity: 1;
      
      &.tn-tips--top {
        transform: translateY(0) translateZ(0) !important;
      }
    }
    
  }
  
</style>