复用面板骨架组件

346 阅读1分钟

面板骨架组件的作用:在页面加载数据之前显示骨架样式

<template>
  <div class="xtx-skeleton shan" :style="{ width: width, height: height }">
    <!-- 1 盒子-->
    <div class="block" :style="{ backgroundColor: bg }"></div>
    <!-- 2 闪效果 xtx-skeleton 伪元素 --->
  </div>
</template>
<script>
export default {
  props: {
    width: {
      type: String,
      default: '100px'
    },
    height: {
      type: String,
      default: '100px'
    },
    bg: {
      type: String,
      default: '#cccs'
    }
  },
  name: 'XtxSkeleton'
}
</script>
<style scoped lang="less">
.xtx-skeleton {
  display: inline-block;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  .block {
    width: 100%;
    height: 100%;
    border-radius: 2px;
  }
}
.shan {
  &::after {
    content: "";
    position: absolute;
    animation: shan 1.5s ease 0s infinite;
    top: 0;
    width: 50%;
    height: 100%;
    background: linear-gradient(
      to left,
      rgba(255, 255, 255, 0) 0,
      rgba(255, 255, 255, 0.3) 50%,
      rgba(255, 255, 255, 0) 100%
    );
    transform: skewX(-45deg);
  }
}
@keyframes shan {
  0% {
    left: -100%;
  }
  100% {
    left: 120%;
  }
}
</style>

以上代码显示效果如下图:

小兔仙效果图.gif

可以传递的值有:宽高背景颜色,可以根据需求再决定别的传递参数

width: {
      type: String,
      default: '100px'
    },
    height: {
      type: String,
      default: '100px'
    },
    bg: {
      type: String,
      default: '#cccs'
    }

使用时,引入这个组件,通过父传子传值即可,注意:骨架组件的外层结构需要跟原结构保持一致