头闻号

天津兴海通达钢铁贸易有限公司

金属建材|焊管|镀锌管|角钢|综合性公司|扁钢

首页 > 新闻中心 > 科技常识:css实现的骨架屏方案
科技常识:css实现的骨架屏方案
发布时间:2023-02-01 10:44:41        浏览次数:4        返回列表

今天小编跟大家讲解下有关css实现的骨架屏方案 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关css实现的骨架屏方案 的相关资料,希望小伙伴们看了有所帮助。

线上体验地址https://jsfiddle.net/z13wtr0q/。先说优缺点,毕竟骨架屏实现的方案有很多种。

优点简单,不需要工程,不用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

原文:https://segmentfault.com/a/1190000020437426

来源:爱蒂网