huangxiaoqiang
2025-05-19 051aa9a0e7f58af6665fb4526e42cf8060fbaa05
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
<template>
  <view
    v-if="show"
    class="tn-skeleton-class tn-skeleton"
    :class="[backgroundColorClass]"
    :style="[skeletonStyle]"
    @touchmove.stop.prevent
  >
    <view
      v-for="(item, index) in rectNodes"
      :key="$t.uuid()"
      class="tn-skeleton__item tn-skeleton__item--rect"
      :class="[elBackgroundColorClass, {'tn-skeleton__item--fade': animation}]"
      :style="[itemStyle('rect', item)]"
    ></view>
    <view
      v-for="(item, index) in circleNodes"
      :key="$t.uuid()"
      class="tn-skeleton__item tn-skeleton__item--circle"
      :class="[elBackgroundColorClass, {'tn-skeleton__item--fade': animation}]"
      :style="[itemStyle('circle', item)]"
    ></view>
    <view
      v-for="(item, index) in filletNodes"
      :key="$t.uuid()"
      class="tn-skeleton__item tn-skeleton__item--fillet"
      :class="[elBackgroundColorClass, {'tn-skeleton__item--fade': animation}]"
      :style="[itemStyle('fillet', item)]"
    ></view>
  </view>
</template>
 
<script>
  import componentsColorMixin from '../../libs/mixin/components_color.js'
  export default {
    name: 'tn-skeleton',
    mixins: [ componentsColorMixin ],
    props: {
      // 显示骨架屏
      show: {
        type: Boolean,
        default: false
      },
      // 需要渲染的元素背景颜色
      elBackgroundColor: {
        type: String,
        default: ''
      },
      // 开启加载动画
      animation: {
        type: Boolean,
        default: true
      },
      // 矩形元素自定义样式
      rectCustomStyle: {
        type: Object,
        default() {
          return {}
        }
      },
      // 圆形元素自定义样式
      circleCustomStyle: {
        type: Object,
        default() {
          return {}
        }
      },
      // 圆角元素自定义样式
      filletCustomStyle: {
        type: Object,
        default() {
          return {}
        }
      }
    },
    computed: {
      elBackgroundColorStyle() {
        return this.$t.color.getBackgroundColorStyle(this.elBackgroundColor)
      },
      elBackgroundColorClass() {
        return this.$t.color.getBackgroundColorInternalClass(this.elBackgroundColor)
      },
      // 骨架屏样式
      skeletonStyle() {
        let style = {}
        style.width = this.skeletonWidth + 'px'
        style.height = this.skeletonHeight + 'px'
        if (this.backgroundColorStyle) {
          style.backgroundColor = this.backgroundColorStyle
        }
        style.left = this.left + 'px'
        style.top = this.top + 'px'
        return style
      },
      // 元素样式
      itemStyle() {
        return (type, item) => {
          let style = {}
          style.width = item.width + 'px'
          style.height = item.height + 'px'
          if (this.elBackgroundColorStyle) {
            style.backgroundColor = this.elBackgroundColorStyle
          }
          style.left = (item.left - this.left) + 'px'
          style.top = (item.top - this.top) + 'px'
          if (type === 'rect') {
            Object.assign(style, this.rectCustomStyle)
          } else if (type === 'circle') {
            style.borderRadius = (item.width / 2) + 'px'
            Object.assign(style, this.circleCustomStyle)
          } else if (type === 'fillet') {
            Object.assign(style, this.filletCustomStyle)
          }
          return style
        }
      }
    },
    data() {
      return {
        // 骨架屏宽度
        skeletonWidth: 750,
        // 骨架屏高度
        skeletonHeight: 1500,
        // 圆角元素
        filletNodes: [],
        // 圆形元素
        circleNodes: [],
        // 矩形元素
        rectNodes: [],
        // 元素偏移位置
        top: 0,
        left: 0
      }
    },
    mounted() {
      this.$nextTick(() => {
        // 获取系统信息
        const systemInfo = uni.getSystemInfoSync()
        this.skeletonWidth = systemInfo.safeArea.width
        this.skeletonHeight = systemInfo.safeArea.height
        this.selectQueryInfo()
      })
    },
    methods: {
      // 查询节点信息
      selectQueryInfo() {
        // 获取整个父容器的宽高作为骨架屏的宽高
        // 在微信小程序中,如果把骨架屏放入组件使用,需要in(this)上下文为父组件才有效
        let query = null
        // 在微信小程序中,如果把骨架屏放入组件使用,需要in(this)上下文为父组件才有效
        // #ifdef MP-WEIXIN
        query = uni.createSelectorQuery().in(this.$parent)
        // #endif
        // #ifndef MP-WEIXIN
        query = uni.createSelectorQuery()
        // #endif
        query.selectAll('.tn-skeleton').boundingClientRect().exec((res) => {
          console.log(res);
          this.skeletonWidth = res[0][0].width
          this.skeletonHeight = res[0][0].height
          this.top = res[0][0].bottom - res[0][0].height
          this.left = res[0][0].left
        })
        
        // 获取元素列表
        this.getRectElements()
        this.getCircleElements()
        this.getFillteElements()
      },
      // 矩形元素列表
      getRectElements() {
        let query = null
        // 在微信小程序中,如果把骨架屏放入组件使用,需要in(this)上下文为父组件才有效
        // #ifdef MP-WEIXIN
        query = uni.createSelectorQuery().in(this.$parent)
        // #endif
        // #ifndef MP-WEIXIN
        query = uni.createSelectorQuery()
        // #endif
        query.selectAll('.tn-skeleton-rect').boundingClientRect().exec((res) => {
          this.rectNodes = res[0]
        })
      },
      // 圆形元素列表
      getCircleElements() {
        let query = null
        // 在微信小程序中,如果把骨架屏放入组件使用,需要in(this)上下文为父组件才有效
        // #ifdef MP-WEIXIN
        query = uni.createSelectorQuery().in(this.$parent)
        // #endif
        // #ifndef MP-WEIXIN
        query = uni.createSelectorQuery()
        // #endif
        query.selectAll('.tn-skeleton-circle').boundingClientRect().exec((res) => {
          this.circleNodes = res[0]
        })
      },
      // 圆角元素列表
      getFillteElements() {
        let query = null
        // 在微信小程序中,如果把骨架屏放入组件使用,需要in(this)上下文为父组件才有效
        // #ifdef MP-WEIXIN
        query = uni.createSelectorQuery().in(this.$parent)
        // #endif
        // #ifndef MP-WEIXIN
        query = uni.createSelectorQuery()
        // #endif
        query.selectAll('.tn-skeleton-fillet').boundingClientRect().exec((res) => {
          this.filletNodes = res[0]
        })
      }
    }
  }
</script>
 
<style lang="scss" scoped>
  .tn-skeleton {
    position: absolute;
    z-index: 9998;
    overflow: hidden;
    background-color: #FFFFFF;
    
    &__item {
      position: absolute;
      background-color: #F0F0F0;
      
      &--fillet {
        border-radius: 10rpx;
      }
      
      &--fade {
        width: 100%;
        height: 100%;
        background-color: #E6E6E6;
        animation-duration: 1.5s;
        animation-name: blink;
        animation-timing-function: ease-in-out;
        animation-iteration-count: infinite;
      }
    }
  }
  
  @keyframes blink {
    0% {
      opacity: 1;
    }
    50% {
      opacity: 0.4;
    }
    100% {
      opacity: 1;
    }
  }
</style>