最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
css如何实现骨架屏 css实现骨架屏代码实例
时间:2022-06-25 18:01:49 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下css实现骨架屏代码实例,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
先说优缺点,毕竟骨架屏实现的方案有很多种
优点
简单,不需要工程,不用puppeteer生成骨架dom,也不需要二次开发维护
定制程度高,想怎么搞就怎么搞
不臃肿,只给你想要的
缺点
自动化程度低,需要在骨架dom上手动添加类
协同要求高,不像工程化能通过工程去约束
思路
通过伪元素实现骨架样式,通过操作样式实现骨架和页面的动态切换
实现
css部分(scss写法)
通过after伪元素生成骨架样式,并通过absolute覆盖到实际元素上
.skt-loading {
pointer-events: none; /* 加载中阻止事件 */
.skeleton {
position: relative;
overflow: hidden;
border: none !important;
border-radius: 5px;
background-color: transparent !important;
background-image: none !important;
&::after {
content: '';
position: absolute;
left: 0;
top: 0;
z-index: 9;
width: 100%;
height: 100%;
background-color: #EBF1F8;
display: block;
}
/* 下面这部分都是自定义的,看需求修改 */
&:not(.not-round)::after {
border-radius: 4px;
}
&:not(.not-before)::before {
position: absolute;
top: 0;
width: 30%;
height: 100%;
content: "";
background: linear-gradient(to right,rgba(255,255,255,0) 0,
rgba(255,255,255,.3) 50%,rgba(255,255,255,0) 100%);
transform: skewX(-45deg);
z-index: 99;
animation: skeleton-ani 1s ease infinite;
display: block;
}
&.badge {
&::after {
background-color: #F8FAFC;
}
}
}
}
@keyframes skeleton-ani { /* 骨架屏动画 */
from {
left: -100%;
}
to {
left: 150%;
}
}
html部分
只需要在你认为合理的骨架粒度元素上添加skeleton类即可
js部分
控制好skt-loading类的切换
使用注意
after伪元素无法插入到inputimg等非容器元素中,所以如果需要添加skleton,则需要再加一层元素将其包裹
对于像vuereact数据驱动页面需要先有mock数据以生成dom
相关文章
- 创世秩序东院藏有秘密哪里有人指导过法详解 11-27
- dnf徐福记觉醒插图外观一览 11-27
- 原神4.3秘宝迷踪第二天攻略 藏宝地位置分享 11-27
- 崩坏星穹铁道黄金与机械难度12通关攻略 11-27
- dnf燃武套武器装扮全职业外观图 11-27
- dnf燃武套红色外观全职业一览 11-27