引言
在页面加载数据时,通常使用占位元素来代替未请求回来数据的元素,用以提升用户体验。而卡片式的占位元素通常称为 Skeleton
即骨架屏。
代码
<div class="card loading">
<div class="image"></div>
<div class="content">
<h4></h4>
<div class="description"></div>
</div>
</div>
:root {
--loading-grey: #eee;
}
/* 卡片基础样式 */
.card {
width: 320px;
background-color: #fff;
overflow: hidden;
border-radius: 6px;
box-shadow: 0px 4px 6px rgba(0, 0, 0, .12);
}
.card .image {
height: 200px;
}
.card .content {
padding: 2rem 1.8rem;
}
.card .content h4 {
margin: 0 0 1rem;
}
/* 卡片 loading 样式 */
.card.loading .content h4 {
min-height: 1.6rem;
border-radius: 4px;
animation-delay: .05s;
}
.card.loading .content .description {
min-height: 4rem;
border-radius: 4px;
animation-delay: .06s;
}
@keyframes loading {
to {
background-position-x: -20%;
}
}
.card.loading .image,
.card.loading .content h4,
.card.loading .content .description {
background-color: var(--loading-grey);
background: linear-gradient(100deg, rgba(255, 255, 255, 0) 40%, rgba(255, 255, 255, .5) 50%, rgba(255, 255, 255, 0) 60%) var(--loading-grey);
background-size: 200% 100%;
background-position-x: 180%;
animation: 1s loading ease-in-out infinite;
}