1. 开场白
上一章我们已经了解了Css Sprite需要的基本CSS语法,那么这一章节我们将带领大家体验一下动画的语法,以及不同动画种类之 间的 区别。
动画通常分为两种形式:一种是过渡动画、另一种是帧动画。
2. 过渡动画
之前我们曾经说过,Css Sprite在帧动画这一领域独领风骚,那么接下来我们就来分析一下动画领域里面常见的两种形式:过渡动画与帧动画之 间的 区别。
首先我们来看看目前各类网站中最常见的一种动画: 过渡动画
<!DOCTYPE html> < html lang = " en " > < head > < Meta charset = " UTF-8 " > < title > Animate-过渡动画 </ title > < style > /* 清除 默 认样式 */ * { padding : ; margin : ; } /* 这段 代码 是为了居中 显示 ,不是重点,看不懂的话可以无视 */ body { height : vh ; dis play : flex ; align-items : center ; justify-content : center ; } .animate { width : px ; height : px ; /* 使用预先定义好的动画,过渡动画 */ animation : change-color s linear infinite alternate ; } /* 定义动画 */ @keyframes change-color { from { background : yellow } to { background : green } } </ style > </ head > < body > < div class = " animate " > </ div > </ body > </ html >
运行结果:
可以看到盒子的颜色是从 黄色 慢慢过渡到 绿色 ,所以叫过渡动画,因为其有 一个 过渡的 效果 。
3. 帧动画
再来看看帧动画是什么样的 效果 :
<!DOCTYPE html> < html lang = " en " > < head > < Meta charset = " UTF-8 " > < title > Animate-帧动画 </ title > < style > /* 清除 默 认样式 */ * { padding : ; margin : ; } /* 这段 代码 是为了居中 显示 ,不是重点,看不懂的话可以无视 */ body { height : vh ; dis play : flex ; align-items : center ; justify-content : center ; } .animate { width : px ; height : px ; /* 使用预先定义好的动画,帧动画 */ animation : change-color s steps ( ) infinite alternate ; } /* 定义动画 */ @keyframes change-color { from { background : yellow } to { background : green } } </ style > </ head > < body > < div class = " animate " > </ div > </ body > </ html >
运行结果:
可以看到是一帧帧播放的,帧数低的时候有种卡卡的感觉,好像一下一下的分步骤从黄色变成绿色的。那我们把帧数提高一下不就看不到一卡一卡的感觉了吗?来试试看:
<!DOCTYPE html> < html lang = " en " > < head > < Meta charset = " UTF-8 " > < title > Animate </ title > < style > /* 清除 默 认样式 */ * { padding : ; margin : ; } /* 这段 代码 是为了居中 显示 ,不是重点,看不懂的话可以无视 */ body { height : vh ; dis play : flex ; align-items : center ; justify-content : center ; } .animate { width : px ; height : px ; /* 使用预先定义好的动画 */ animation : change-color s steps ( ) infinite alternate ; } /* 定义动画 */ @keyframes change-color { from { background : yellow } to { background : green } } </ style > </ head > < body > < div class = " animate " > </ div > </ body > </ html >
运行结果:
虽然 效果 一样了,但是怎么感觉更麻烦了呢?还要自己去指定帧数,而过渡动画都是全 自动 的,帧动画是不是不如过渡动画呢?实际上并不是这样的,帧动画有着自己的适用场景。接下来我们就来探讨一下何时适合帧动画,何时又适合过渡动画。
4. 小结
乍一看好像过渡动画更胜一筹,但实际上他们两个各自有各自的适用场景。
下一章我们就来看看什么样的场景适合过渡动画。
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did100552