本文作者:George Phillips
翻譯自:原文地址
Facebook首頁(yè)仪芒,鼠標(biāo)滾動(dòng)到底蛮粮,實(shí)時(shí)加載新動(dòng)態(tài)穆咐,加載過(guò)程會(huì)是下面的動(dòng)態(tài)效果关拒。這種效果不是gif圖,而是CSS動(dòng)畫(huà)實(shí)現(xiàn)庸娱。下面剖析如何實(shí)現(xiàn)着绊。
HTML
<div class="timeline-wrapper">
<div class="timeline-item">
<div class="animated-background">
<div class="background-masker header-top"></div>
<div class="background-masker header-left"></div>
<div class="background-masker header-right"></div>
<div class="background-masker header-bottom"></div>
<div class="background-masker subheader-left"></div>
<div class="background-masker subheader-right"></div>
<div class="background-masker subheader-bottom"></div>
<div class="background-masker content-top"></div>
<div class="background-masker content-first-end"></div>
<div class="background-masker content-second-line"></div>
<div class="background-masker content-second-end"></div>
<div class="background-masker content-third-line"></div>
<div class="background-masker content-third-end"></div>
</div>
</div>
</div>
外部容器
最外面是包裹這段動(dòng)畫(huà)的居中div容器。
.timeline-item {
background: #fff;
border: 1px solid;
border-color: #e5e6e9 #dfe0e4 #d0d1d5;
border-radius: 3px;
padding: 12px;
margin: 0 auto;
max-width: 472px;
min-height: 200px;
}
動(dòng)態(tài)背景
接下來(lái)實(shí)現(xiàn)動(dòng)態(tài)背景效果熟尉,使用CSS animation和背景漸變linear-gradient归露。
@keyframes placeHolderShimmer{
0%{
background-position: -468px 0
}
100%{
background-position: 468px 0
}
}
.animated-background {
animation-duration: 1s;
animation-fill-mode: forwards;
animation-iteration-count: infinite;
animation-name: placeHolderShimmer;
animation-timing-function: linear;
background: #f6f7f8;
background: linear-gradient(to right, #eeeeee 8%, #dddddd 18%, #eeeeee 33%);
background-size: 800px 104px;
height: 96px;
position: relative;
}
添加大量阻擋住背景的div
到現(xiàn)在效果看起來(lái),就像一個(gè)巨大的進(jìn)度條斤儿。我們添加大量的div剧包,用來(lái)遮擋下面的背景動(dòng)態(tài)效果。
.background-masker {
background: #fff;
position: absolute;
}
/* Every thing below this is just positioning */
.background-masker.header-top,
.background-masker.header-bottom,
.background-masker.subheader-bottom {
top: 0;
left: 40px;
right: 0;
height: 10px;
}
.background-masker.header-left,
.background-masker.subheader-left,
.background-masker.header-right,
.background-masker.subheader-right {
top: 10px;
left: 40px;
height: 8px;
width: 10px;
}
.background-masker.header-bottom {
top: 18px;
height: 6px;
}
.background-masker.subheader-left,
.background-masker.subheader-right {
top: 24px;
height: 6px;
}
.background-masker.header-right,
.background-masker.subheader-right {
width: auto;
left: 300px;
right: 0;
}
.background-masker.subheader-right {
left: 230px;
}
.background-masker.subheader-bottom {
top: 30px;
height: 10px;
}
.background-masker.content-top,
.background-masker.content-second-line,
.background-masker.content-third-line,
.background-masker.content-second-end,
.background-masker.content-third-end,
.background-masker.content-first-end {
top: 40px;
left: 0;
right: 0;
height: 6px;
}
.background-masker.content-top {
height:20px;
}
.background-masker.content-first-end,
.background-masker.content-second-end,
.background-masker.content-third-end{
width: auto;
left: 380px;
right: 0;
top: 60px;
height: 8px;
}
.background-masker.content-second-line {
top: 68px;
}
.background-masker.content-second-end {
left: 420px;
top: 74px;
}
.background-masker.content-third-line {
top: 82px;
}
.background-masker.content-third-end {
left: 300px;
top: 88px;
}
結(jié)語(yǔ)
當(dāng)網(wǎng)站的數(shù)據(jù)異步加載時(shí)往果,給與用戶(hù)加載的指示疆液,讓人心理上覺(jué)得等待時(shí)間變短,更傾向于繼續(xù)留在網(wǎng)頁(yè)上瀏覽陕贮。
你可以點(diǎn)擊鏈接在線(xiàn)查看效果:live demo堕油。