uni-app 骨架屏实现

1.创建一个骨架屏组件skeleton.vue

<template>
  <div>
    <div class="wrap"
         :style="{'width':systemInfo.width+'px','height':systemInfo.height+'px', 'background-color':bgcolor}">
      <div v-for="(item,index) in skeletonRectLists"
           :index='index'
           :key='index'
           class="chiaroscuro"
           :style="{'width':item.width+'px','height':item.height+'px','background-color':'rgba(233, 2, 233,1)','position':'absolute','left':item.left+'px','top':item.top+'px'}">
      </div>
      <div v-for="(item,index) in skeletonCircleLists"
           :index='index'
           :key="'info2-'+index"
           class="chiaroscuro"
           :style="{'width':item.width+'px','height':item.height+'px','background-color':'rgba(233, 2, 233,1)','border-radius':'24rpx','position':'absolute','left':item.left+'px','top':item.top+'px'}">
      </div>
    </div>
  </div>
</template>

<script>
        /* eslint-disable */
        export default {
          props: {
            bgcolor: { type: String, value: '#FFF' },
            selector: { type: String, value: 'skeleton' },
          },
        
          data() {
            return {
              systemInfo: {},
              skeletonRectLists: [],
              skeletonCircleLists: [],
            };
          },
        
          components: {},
        
          methods: {
            rectHandle: function() {
              const that = this;
              //绘制不带样式的节点
              uni.createSelectorQuery().selectAll(`.${this.selector}-rect`).boundingClientRect().exec(function(res) {
                  that.skeletonRectLists = res[0];
                });
            },
        
            radiusHandle: function() {
              const that = this;
              uni.createSelectorQuery().selectAll(`.${this.selector}-radius`).boundingClientRect().exec(function(res) {
                    console.log(res[0].length);
                  that.skeletonCircleLists = res[0];
                });
            },
          },
        
          mounted: function() {
            //默认的首屏宽高,防止内容闪现
            const systemInfo = uni.getSystemInfoSync();
            (this.systemInfo = {
              width: systemInfo.windowWidth,
              height: systemInfo.windowHeight,
            });
            const that = this;
            //绘制背景
            uni.createSelectorQuery().selectAll(`.${this.selector}`).boundingClientRect().exec(function(res) {
                that.systemInfo.height = res[0][0].height + res[0][0].top || 0;
              });
        
            //绘制矩形
            this.rectHandle();
        
            //绘制圆形
            this.radiusHandle();
          },
        };
        /* eslint-enable */
</script>

<style scoped>
        .wrap {
          position: absolute;
          left: 0;
          top: 0;
          z-index: 9998;
          overflow: hidden;
        }
        .chiaroscuro {
          animation-duration: 1s;
          animation-fill-mode: forwards;
          animation-iteration-count: infinite;
          animation-name: placeHolderShimmer;
          animation-timing-function: linear;
          background: #f6f7f8;
          background: linear-gradient(to right, #eeeeee 8%, #dddddd 18%, #eeeeee 33%);
          background-size: 800px 104px;
          height: 40px;
          position: relative;
        }
        @keyframes placeHolderShimmer{
          0% {
            background-position: -468px 0
          }
          100% {
            background-position: 468px 0
          }
        }
</style>

 2.引入骨架屏

import skeleton from '../../components/skeleton/skeleton.vue'
	export default {
		components: {
			skeleton: skeleton
		},
        data() {
			return {
		        showSkeleton: true,
            }
        },
        onLoad() {
			this.$nextTick(() => {
				this.reloadData();
			})
		},
        methods: {
			reloadData() {
				setTimeout(() => {
					this.showSkeleton = false
				}, 300)
			},
        }
    }

3. 页面使用

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值