骨架屏组件的封装
一个纯UI组件,提供数据返回之前的占位
1. 搭建一个最基础的静态组件
2. 考虑哪些属性可以由用户自定义 width height background
3. 定义具体的props传入 (类型 默认值 是否必须)
小坑: 当props作为样式使用的时候需要注意传入的数据必须是合法的css值
优化: 暴露给用户的是number类型 在内部做拼接处理
<template>
<div
class="xtx-skeleton shan"
:style="{ width, height }"
>
<!-- 1 盒子-->
<div class="block" :style="{ backgroundColor:bg }"></div>
<!-- 2 闪效果 xtx-skeleton 伪元素 --->
</div>
</template>
<script>
export default {
name: 'XtxSkeleton',
props: {
width: {
type: String,
default: '60px'
},
height: {
type: String,
default: '30px'
},
bg: {
type: String,
default: ' #efefef'
}
}
}
</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>
在数据加载完成之前,呈现以下情况