CSS3 animation实现逐帧动画效果示例介绍
这篇文章主要介绍了CSS3 animation实现逐帧动画效果示例介绍,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
css3里面的animation属性非常强大,但是自己用的比较少,最近有次面试就刚好被问到了,趁现在有时间就对animation做一个小总结。同时实现一个逐帧动画的demo作为练习
animation属性一览
因为animation属性比较多,然后在w3c上看有点蛋疼,干脆也做了一份导图,以后想查看,就一目了然了
使用animation实现逐帧动画
熟悉了animation的属性之后,得找个简单的小项目实现下,逐帧动画好有意思,先跑一个满足下自己
思路很简单,就是给元素一个雪碧图的背景,然后添加的帧动画更改background-position,关键代码:
@keyframes run{ from{ background-position: 0 0; } to{ background-position: -1540px 0 ; } } p{ width:140px; height:140px; background: url(run.png) ; animation-name:run; animation-duration:1s; animation-iteration-count:infinite; }
但是跑起来后我们发现,每帧动画之间帧动画都是滑动,并不是我们要的效果,为什么呢?
原来animation默认以ease方式过渡,它会在每个关键帧之间插入补间动画,所以动画效果是连贯性的
知道原因就好办了,解决思路就是:
@keyframes run{ 0%, 8%{ /*动作一*/ } 9.2%, 17.2%{ /*动作二*/ } ... }
step1:动作之间停留8帧,0%设置动作一,动作一结束在8%
step2:动作之间过渡1.2帧,9.2%设置动作二,动作二结束在17.2%
完整代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>css3逐帧动画</title> <style> @keyframes run{ 0%, 8%{ background-position: 0 0; } 9.2%, 17.2%{ background-position: -140px 0; } 18.4%, 26.4%{ background-position: -280px 0 ; } 27.6%, 35.6%{ background-position: -420px 0 ; } 36.8%, 44.8%{ background-position: -560px 0 ; } 46%, 54%{ background-position: -700px 0 ; } 55.2%, 63.2%{ background-position: -840px 0 ; } 64.4%, 72.4%{ background-position: -980px 0 ; } 73.6%, 81.6%{ background-position: -1120px 0 ; } 82.8%, 90.8%{ background-position: -1400px 0 ; } 92%, 100%{ background-position: -1540px 0 ; } } @-webkit-keyframes run{ 0%, 8%{ background-position: 0 0; } 9.2%, 17.2%{ background-position: -140px 0; } 18.4%, 26.4%{ background-position: -280px 0 ; } 27.6%, 35.6%{ background-position: -420px 0 ; } 36.8%, 44.8%{ background-position: -560px 0 ; } 46%, 54%{ background-position: -700px 0 ; } 55.2%, 63.2%{ background-position: -840px 0 ; } 64.4%, 72.4%{ background-position: -980px 0 ; } 73.6%, 81.6%{ background-position: -1120px 0 ; } 82.8%, 90.8%{ background-position: -1400px 0 ; } 92%, 100%{ background-position: -1540px 0 ; } } p{ width:140px; height:140px; background: url(blog/754767/201606/754767-20160601000042992-1734972084.png) ; animation:run 1s infinite; -webkit-animation:run 1s infinite; animation-fill-mode : backwards; -webkit-animation-fill-mode : backwards; } </style> </head> <body> <p></p> </body> </html>
还有另外一个实现方法,就是利用steps(),就是帧之间的阶跃动画,这个在w3c里面没有写,先贴个图
由上图可知:
steps(1,start):动画一开始就跳到 100% 直到这一帧(不是整个周期)结束
steps(1,end):保持 0% 的样式直到这一帧(不是整个周期)结束
另外也可以直接设置 animation-timing-function:step-start/step-end
step-start效果等同于steps(1,start),step-end效果等同于steps(1,end)
最终效果,因为录制的问题可能有点卡顿,有兴趣的同学可以直接复制代码去跑下:
完整代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>css3逐帧动画</title> <style> @keyframes run{ 0%{ background-position: 0 0; } 8.333%{ background-position: -140px 0; } 16.666%{ background-position: -280px 0 ; } 25.0%{ background-position: -420px 0 ; } 33.333%{ background-position: -560px 0 ; } 41.666%{ background-position: -700px 0 ; } 50.0%{ background-position: -840px 0 ; } 58.333%{ background-position: -980px 0 ; } 66.666%{ background-position: -1120px 0 ; } 75.0%{ background-position: -1260px 0 ; } 83.333%{ background-position: -1400px 0 ; } 91.666%{ background-position: -1540px 0 ; } 100%{ background-position: 0 0 ; } } @-webkit-keyframes run{ 0%{ background-position: 0 0; } 8.333%{ background-position: -140px 0; } 16.666%{ background-position: -280px 0 ; } 25.0%{ background-position: -420px 0 ; } 33.333%{ background-position: -560px 0 ; } 41.666%{ background-position: -700px 0 ; } 50.0%{ background-position: -840px 0 ; } 58.333%{ background-position: -980px 0 ; } 66.666%{ background-position: -1120px 0 ; } 75.0%{ background-position: -1260px 0 ; } 83.333%{ background-position: -1400px 0 ; } 91.666%{ background-position: -1540px 0 ; } 100%{ background-position: 0 0 ; } } p{ width:140px; height:140px; background: url(754767/201606/754767-20160601000042992-1734972084.png) ; animation:run 1s steps(1, start) infinite; -webkit-animation:run 1s steps(1, start) infinite; } </style> </head> <body> <p></p> </body>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。
以上是CSS3 animation实现逐帧动画效果示例介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

在 Vue.js 中使用 Bootstrap 分为五个步骤:安装 Bootstrap。在 main.js 中导入 Bootstrap。直接在模板中使用 Bootstrap 组件。可选:自定义样式。可选:使用插件。

HTML定义网页结构,CSS负责样式和布局,JavaScript赋予动态交互。三者在网页开发中各司其职,共同构建丰富多彩的网站。

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

创建 Bootstrap 分割线有两种方法:使用 标签,可创建水平分割线。使用 CSS border 属性,可创建自定义样式的分割线。

在 Bootstrap 中插入图片有以下几种方法:直接插入图片,使用 HTML 的 img 标签。使用 Bootstrap 图像组件,可以提供响应式图片和更多样式。设置图片大小,使用 img-fluid 类可以使图片自适应。设置边框,使用 img-bordered 类。设置圆角,使用 img-rounded 类。设置阴影,使用 shadow 类。调整图片大小和位置,使用 CSS 样式。使用背景图片,使用 background-image CSS 属性。

如何使用 Bootstrap 按钮?引入 Bootstrap CSS创建按钮元素并添加 Bootstrap 按钮类添加按钮文本

要设置 Bootstrap 框架,需要按照以下步骤:1. 通过 CDN 引用 Bootstrap 文件;2. 下载文件并将其托管在自己的服务器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根据需要编译 Sass/Less;5. 导入定制文件(可选)。设置完成后,即可使用 Bootstrap 的网格系统、组件和样式创建响应式网站和应用程序。

要调整 Bootstrap 中元素大小,可以使用尺寸类,具体包括:调整宽度:.col-、.w-、.mw-调整高度:.h-、.min-h-、.max-h-
