淺談只要css就能實(shí)現(xiàn)的骨架屏方案
發(fā)布時(shí)間:2019-09-20 15:21:22 作者:Gauch
我要評(píng)論
這篇文章主要介紹了淺談只要css就能實(shí)現(xiàn)的骨架屏方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
線上體驗(yàn)地址 https://jsfiddle.net/z13wtr0q/
先說(shuō)優(yōu)缺點(diǎn),畢竟骨架屏實(shí)現(xiàn)的方案有很多種
優(yōu)點(diǎn)
- 簡(jiǎn)單,不需要工程,不用puppeteer生成骨架dom,也不需要二次開(kāi)發(fā)維護(hù)
- 定制程度高,想怎么搞就怎么搞
- 不臃腫,只給你想要的
缺點(diǎn)
- 自動(dòng)化程度低,需要在骨架dom上手動(dòng)添加類(lèi)
- 協(xié)同要求高,不像工程化能通過(guò)工程去約束
思路
通過(guò)偽元素實(shí)現(xiàn)骨架樣式,通過(guò)操作樣式實(shí)現(xiàn)骨架和頁(yè)面的動(dòng)態(tài)切換
實(shí)現(xiàn)
css部分(scss寫(xiě)法)
通過(guò)after偽元素生成骨架樣式,并通過(guò)absolute覆蓋到實(shí)際元素上
.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 { /* 骨架屏動(dòng)畫(huà) */
from {
left: -100%;
}
to {
left: 150%;
}
}
html部分
只需要在你認(rèn)為合理的骨架粒度元素上添加skeleton類(lèi)即可
js部分
控制好skt-loading類(lèi)的切換
使用注意
- after偽元素?zé)o法插入到inputimg等非容器元素中,所以如果需要添加skleton,則需要再加一層元素將其包裹
- 對(duì)于像vuereact數(shù)據(jù)驅(qū)動(dòng)頁(yè)面需要先有mock數(shù)據(jù)以生成dom
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章

CSS實(shí)現(xiàn)Skeleton Screen骨架屏效果
這篇文章主要介紹了CSS實(shí)現(xiàn)Skeleton Screen骨架屏效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-16
這篇文章主要介紹了使用CSS自定義屬性實(shí)現(xiàn)骨架屏效果,在網(wǎng)上可以看到骨架屏的使用已經(jīng)非常廣泛,F(xiàn)acebook,Google,Slack等公司都在使用,本文通過(guò)示例代碼給大家介紹的非2022-06-20



