利用CSS也能輕松實(shí)現(xiàn)超酷炫的轉(zhuǎn)場(chǎng)動(dòng)畫(huà)了!只需利用最新的 CSS @scroll-timeline 規(guī)范即可。下面本篇文章就通過(guò)兩個(gè)案例來(lái)看看利用@scroll-timeline怎么實(shí)現(xiàn)超酷炫的轉(zhuǎn)場(chǎng)動(dòng)畫(huà),希望對(duì)大家有所幫助!
在 WeGame 的 PC 端官網(wǎng)首頁(yè),有著非常多制作精良的基于滾動(dòng)的動(dòng)畫(huà)效果。
這里我簡(jiǎn)單截取其中 2 個(gè)比較有意思的轉(zhuǎn)場(chǎng)動(dòng)畫(huà),大家感受感受。轉(zhuǎn)場(chǎng)動(dòng)畫(huà) 1:
轉(zhuǎn)場(chǎng)動(dòng)畫(huà) 2:
是不是挺有意思的,整個(gè)動(dòng)畫(huà)的銜接是基于滾輪的滾動(dòng)觸發(fā)的。我猜測(cè)是使用了類(lèi)似 TweenMaxJS 的動(dòng)畫(huà)庫(kù)實(shí)現(xiàn)。
當(dāng)然,這兩處酷炫有意思的轉(zhuǎn)場(chǎng)動(dòng)畫(huà),基于最新的 CSS @scroll-timeline 規(guī)范,也是可以大致實(shí)現(xiàn)的。本文就將嘗試使用純 CSS,模擬上述的兩個(gè)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)。【推薦學(xué)習(xí):css視頻教程】
當(dāng)然,關(guān)于 CSS 最新的 CSS @scroll-timeline 規(guī)范,如果你還沒(méi)有詳細(xì)了解過(guò),可以先看看我的這篇文章 來(lái)了來(lái)了,它終于來(lái)了,動(dòng)畫(huà)殺手锏 @scroll-timeline
轉(zhuǎn)場(chǎng)動(dòng)畫(huà)一
首先,我們來(lái)看看這個(gè)動(dòng)畫(huà):
核心步驟拆解一下:
-
處于場(chǎng)景 1,接著借助 WeGame 的 LOGO,LOGO 開(kāi)始放大
-
LOGO 放大到一定程度,開(kāi)始漸隱,LOGO 背后的場(chǎng)景 2 逐漸漸現(xiàn)
-
LOGO 放大且漸隱消失,場(chǎng)景 2 完全出現(xiàn)
這里,要實(shí)現(xiàn)整個(gè)動(dòng)畫(huà),有一個(gè)非常重要的場(chǎng)景,就是能夠利用 LOGO 元素,切割背景,只看到 LOGO 背后的元素,像是得到一張這樣的圖片:
注意,圖片的白色部分,不是白色,而是需要透明,能夠透出背后的元素。
當(dāng)然,我們可以讓 UI 切一張這樣的圖出來(lái),但是畢竟太麻煩了。
假設(shè)我們只有一張 LOGO 元素:
我們?nèi)绾文軌蚪柚@個(gè) LOGO,切割背景呢?
借助 mask 及 mask-composite 切割背景
是的,這里我們可以使用 mask
。我們來(lái)嘗試一下:
<div></div>
div { background: linear-gradient(-75deg, #715633, #2b2522); }
假設(shè)我們有這樣一張背景:
我們使用 LOGO 圖作為 MASK,對(duì)該背景進(jìn)行切割:
div { background: linear-gradient(-75deg, #715633, #2b2522); mask: url(WeGame-LOGO圖.png); mask-repeat: no-repeat; mask-position: center center; }
我們會(huì)得到這樣一張圖:
Oh No,這與我們想象的剛好相反,我們要的是 LOGO 處透明,背景的其他處保留。
怎么做呢?不要慌,這里可以使用上我們上一篇文章介紹過(guò)的 -webkit-mask-composite
,還不太了解的可以戳這里看看:高階切圖技巧!基于單張圖片的任意顏色轉(zhuǎn)換
我們簡(jiǎn)單改造一下代碼:
div { background: linear-gradient(-75deg, #715633, #2b2522); mask: url(//wegame.gtimg.com/g.55555-r.c4663/wegame-home/sc01-logo.52fe03c4.svg), linear-gradient(#fff, #fff); mask-repeat: no-repeat; mask-position: center center; -webkit-mask-composite: xor; }
這樣,我們能就順利的得到了這樣一張圖形:
當(dāng)然這里需要注意的是,白色區(qū)域并非白色,而是透明的,可以透出背后的內(nèi)容。
配合 @scroll-timeline
好,如此一來(lái),基于上述的剪切層,再配合 @scroll-timeline
,我們來(lái)模擬一個(gè)最基本的動(dòng)畫(huà)效果:
<div class="g-scroll" id="g-scroll"></div> <div class="g-wrap"> <div class="g-bg"></div> <div class="g-container"> <div class="g-wegame"></div> </div> </div>
.g-scroll { position: relative; width: 100vw; height: 500vh; } .g-wrap { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; overflow: hidden; } .g-container { position: absolute; top: 0; left: 0; width: 100vw; height: 100vh; animation-name: scale; animation-duration: 10s; animation-timeline: box-move; } .g-bg { position: fixed; width: 100vw; height: 100vh; background: url(LOGO背后的圖層); } .g-wegame { position: absolute; width: 100vw; height: 100vh; background: linear-gradient(-75deg, #715633, #2b2522); mask: url(//wegame.gtimg.com/g.55555-r.c4663/wegame-home/sc01-logo.52fe03c4.svg), linear-gradient(#fff, #fff); mask-repeat: no-repeat; mask-position: center center; -webkit-mask-composite: xor; } @scroll-timeline box-move { source: selector("#g-scroll"); orientation: "vertical"; } @keyframes scale { 0% { transform: scale(1); } 100% { transform: scale(60); } }
這里,想要看懂上述代碼,你必須已經(jīng)掌握了基本的 CSS @scroll-timeline 語(yǔ)法。其余的內(nèi)容,簡(jiǎn)單解釋下:
-
我們?cè)?LOGO 后面的圖層,用
.g-bg
使用一張圖片表示了場(chǎng)景 2 -
#g-scroll
用于基于滾動(dòng)條的滾動(dòng),實(shí)現(xiàn)滾動(dòng)動(dòng)畫(huà) -
.g-wegame
里面就是上述使用mask
和mask-composite
實(shí)現(xiàn)的圖層
好,此時(shí),我們向下滾動(dòng)動(dòng)畫(huà),就會(huì)觸發(fā) .g-container
的動(dòng)畫(huà),也就是從 transform: scale(1)
到 transform: scale(60)
,我們來(lái)看看效果:
有點(diǎn)那個(gè)意思了。但是,這里還缺少了一些細(xì)節(jié)。
首先我們需要有一個(gè) LOGO,它的透明度從 1 逐漸漸隱到 0,這個(gè)比較簡(jiǎn)單,加完之后,我們看看效果:
離目標(biāo)又近了一步,但是,仔細(xì)觀察原效果,我們還少了一層:
在 LOGO 漸隱的過(guò)程中,背后的背景不是直接呈現(xiàn)的,而是有一個(gè)漸現(xiàn)的過(guò)程。所以,完整而言,在動(dòng)畫(huà)過(guò)程從,一共會(huì)有 4 層:
所以,完整的代碼,大概是這樣的:
<div class="g-scroll" id="g-scroll"></div> <div class="g-wrap"> <div class="g-bg"></div> <div class="g-container"> <div class="g-wegame"></div> <div class="g-mask"></div> <div class="g-logo"></div> </div> </div>
.g-scroll { position: relative; width: 100vw; height: 500vh; } .g-wrap { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; overflow: hidden; } .g-container { position: absolute; top: 0; left: 0; width: 100vw; height: 100vh; animation-name: scale; animation-duration: 10s; animation-timeline: box-move; } .g-bg { position: fixed; width: 100vw; height: 100vh; background: url(//背景圖片,場(chǎng)景2); } .g-wegame { position: absolute; width: 100vw; height: 100vh; background: linear-gradient(-75deg, #715633, #2b2522); mask: url(//WeGame-Logo.png), linear-gradient(#fff, #fff); mask-repeat: no-repeat; mask-position: center center; -webkit-mask-composite: xor; z-index: 1; } .g-mask { position: aboslute; width: 100vw; height: 100vh; background: linear-gradient(-75deg, #715633, #2b2522); z-index: 2; animation-name: reOpacityChange; animation-duration: 10s; animation-timeline: box-move; animation-function-timing: linear; } .g-logo { position: absolute; background: url(//WeGame-Logo.png); background-repeat: no-repeat; background-position: center center; z-index: 3; animation-name: reOpacityChange; animation-duration: 10s; animation-timeline: box-move; } @scroll-timeline box-move { source: selector("#g-scroll"); orientation: "vertical"; } @keyframes reOpacityChange { 0%, 50% { opacity: 1; } 100% { opacity: 0; } } @keyframes scale { 0% { transform: scale(1); } 100% { transform: scale(60); } }
這樣,我們就基本能夠還原原效果了:
完整的代碼,你可以戳這里:CodePen Demo – WeGame Animation Demo
轉(zhuǎn)場(chǎng)動(dòng)畫(huà)二
好,搞定了一個(gè),我們繼續(xù)來(lái)看下一個(gè):
這里,我們也簡(jiǎn)單拆解下動(dòng)畫(huà):
-
數(shù)字放大,逐漸帶出場(chǎng)景 2
-
場(chǎng)景 2 有一個(gè)非??犰诺墓庥笆湛s效果
這里的數(shù)字放大與第一個(gè)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)其實(shí)非常類(lèi)似,就不詳細(xì)講了。
我們來(lái)看看,在場(chǎng)景 2 這里,光影的收縮效果如何實(shí)現(xiàn)。
這里看似負(fù)責(zé),但是,其實(shí)非常的簡(jiǎn)單。這里,核心在于這兩張圖片:
圖片素材 1:
注意,這里最為核心的在于,圖片中的白色不是白色,是透明的,可以透出背景的內(nèi)容。
這樣,我們只需要在這張圖片的背后,放置另外這樣一張圖片:
想到了嗎?沒(méi)錯(cuò),就是讓這張圖片從一個(gè)較大的 transform: scale()
值,變化到一個(gè)較小的 transform: scale()
值即可!
什么意思呢?看看這張圖你就懂了:
知道了解到這一點(diǎn),整個(gè)動(dòng)畫(huà)也就比較簡(jiǎn)單了。當(dāng)然,這里我們也同樣借助了 CSS @scroll-timeline 完成整個(gè)動(dòng)畫(huà):
<div class="g-scroll" id="g-scroll"></div> <div class="g-container"> <div class="g-bg"></div> <div class="g-circle"></div> <div class="g-word">30</div> </div>
.g-scroll { position: relative; width: 100vw; height: 500vh; } .g-container { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; overflow: hidden; } .g-bg { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: url(//蜂巢圖片.png); z-index: 1; } .g-circle { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) scale(.5); width: 400px; height: 400px; background: url(//光圈圖片.png); animation-name: scale; animation-duration: 10s; animation-timeline: box-move; } .g-word { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 12vw; z-index: 10; color: transparent; background: linear-gradient(#f8a011, #ffd973); background-clip: text; animation-name: scaleWord; animation-duration: 10s; animation-timeline: box-move; } @scroll-timeline box-move { source: selector("#g-scroll"); orientation: "vertical"; } @keyframes scale { 0% { transform: translate(-50%, -50%) scale(10); } 100% { transform: translate(-50%, -50%) scale(.5); } } @keyframes scaleWord { 0% { transform: translate(-50%, -50%) scale(.5); } 100% { transform: translate(calc(-50% - 5000px), -50%) scale(100); } }
整個(gè)動(dòng)畫(huà)需要看懂,其實(shí)還是要有一定的功底的。上效果:
完整的代碼,你可以戳這里:CodePen Demo – WeGame Animation Demo
這樣,借助強(qiáng)大的 CSS 以及一些有意思的技巧,我們利用純 CSS 實(shí)現(xiàn)了這兩個(gè)看似非常復(fù)雜的轉(zhuǎn)場(chǎng)動(dòng)畫(huà)效果,并且,這在之前,是完全不可能使用純 CSS 實(shí)現(xiàn)的。
原文地址:https://segmentfault.com/a/1190000042184211
作者:chokcoco
(學(xué)習(xí)視頻分享:web前端)