在 WeGame 的 PC 端官网首页,有着十分多制作精良的基于滚动的动画成果。
这里我简略截取其中 2 个比拟有意思的转场动画,大家感触感触。转场动画 1:
转场动画 2:
是不是挺有意思的,整个动画的连接是基于滚轮的滚动触发的。我猜想是应用了相似 TweenMaxJS 的动画库实现。
当然,这两处酷炫有意思的转场动画,基于最新的 CSS @scroll-timeline 标准,也是能够大抵实现的。本文就将尝试应用纯 CSS,模仿上述的两个转场动画。
当然,对于 CSS 最新的 CSS @scroll-timeline 标准,如果你还没有具体理解过,能够先看看我的这篇文章 来了来了,它终于来了,动画杀手锏 @scroll-timeline
转场动画一
首先,咱们来看看这个动画:
外围步骤拆解一下:
- 处于场景 1,接着借助 WeGame 的 LOGO,LOGO 开始放大
- LOGO 放大到肯定水平,开始渐隐,LOGO 背地的场景 2 逐步渐现
- LOGO 放大且渐隐隐没,场景 2 齐全呈现
这里,要实现整个动画,有一个十分重要的场景,就是可能利用 LOGO 元素,切割背景,只看到 LOGO 背地的元素,像是失去一张这样的图片:
留神,图片的红色局部,不是红色,而是须要通明,可能透出背地的元素。
当然,咱们能够让 UI 切一张这样的图进去,然而毕竟太麻烦了。
假如咱们只有一张 LOGO 元素:
咱们如何可能借助这个 LOGO,切割背景呢?
借助 mask 及 mask-composite 切割背景
是的,这里咱们能够应用 mask
。咱们来尝试一下:
<div></div>
div { background: linear-gradient(-75deg, #715633, #2b2522);}
假如咱们有这样一张背景:
咱们应用 LOGO 图作为 MASK,对该背景进行切割:
div { background: linear-gradient(-75deg, #715633, #2b2522); mask: url(WeGame-LOGO图.png); mask-repeat: no-repeat; mask-position: center center;}
咱们会失去这样一张图:
Oh No,这与咱们设想的刚好相同,咱们要的是 LOGO 处通明,背景的其余处保留。
怎么做呢?不要慌,这里能够应用上咱们上一篇文章介绍过的 -webkit-mask-composite
,还不太理解的能够戳这里看看:高阶切图技巧!基于单张图片的任意色彩转换
咱们简略革新一下代码:
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;}
这样,咱们能就顺利的失去了这样一张图形:
当然这里须要留神的是,红色区域并非红色,而是通明的,能够透出背地的内容。
配合 @scroll-timeline
好,如此一来,基于上述的剪切层,再配合 @scroll-timeline
,咱们来模仿一个最根本的动画成果:
<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); }}
这里,想要看懂上述代码,你必须曾经把握了根本的 CSS @scroll-timeline 语法。其余的内容,简略解释下:
- 咱们在 LOGO 前面的图层,用
.g-bg
应用一张图片示意了场景 2 #g-scroll
用于基于滚动条的滚动,实现滚动动画.g-wegame
外面就是上述应用mask
和mask-composite
实现的图层
好,此时,咱们向下滚动动画,就会触发 .g-container
的动画,也就是从 transform: scale(1)
到 transform: scale(60)
,咱们来看看成果:
有点那个意思了。然而,这里还短少了一些细节。
首先咱们须要有一个 LOGO,它的透明度从 1 逐步渐隐到 0,这个比较简单,加完之后,咱们看看成果:
离指标又近了一步,然而,仔细观察原成果,咱们还少了一层:
在 LOGO 渐隐的过程中,背地的背景不是间接出现的,而是有一个渐现的过程。所以,残缺而言,在动画过程从,一共会有 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(//背景图片,场景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
转场动画二
好,搞定了一个,咱们持续来看下一个:
这里,咱们也简略拆解下动画:
- 数字放大,逐步带出场景 2
- 场景 2 有一个十分酷炫的光影膨胀成果
这里的数字放大与第一个转场动画其实十分相似,就不具体讲了。
咱们来看看,在场景 2 这里,光影的膨胀成果如何实现。
这里看似负责,然而,其实十分的简略。这里,外围在于这两张图片:
图片素材 1:
留神,这里最为外围的在于,图片中的红色不是红色,是通明的,能够透出背景的内容。
这样,咱们只须要在这张图片的背地,搁置另外这样一张图片:
想到了吗?没错,就是让这张图片从一个较大的 transform: scale()
值,变动到一个较小的 transform: scale()
值即可!
什么意思呢?看看这张图你就懂了:
晓得理解到这一点,整个动画也就比较简单了。当然,这里咱们也同样借助了 CSS @scroll-timeline 实现整个动画:
<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); }}
整个动画须要看懂,其实还是要有肯定的功底的。上成果:
残缺的代码,你能够戳这里:CodePen Demo - WeGame Animation Demo
这样,借助弱小的 CSS 以及一些有意思的技巧,咱们利用纯 CSS 实现了这两个看似非常复杂的转场动画成果,并且,这在之前,是齐全不可能应用纯 CSS 实现的。
最初
本文到此结束,心愿对你有帮忙 :)
更多精彩 CSS 技术文章汇总在我的 Github -- iCSS ,继续更新,欢送点个 star 订阅珍藏。
如果还有什么疑难或者倡议,能够多多交换,原创文章,文笔无限,满腹经纶,文中若有不正之处,万望告知。