CSS3动画效果之animation
先解决上一篇的遗留问题。
div {
width: 300px;
height: 200px;
background-color: red;
-webkit-animation: test1 2s;
}
@-webkit-keyframes test1 {
from {background-color: green;}
to {background-color: red;}
}
这样定义的动画,实际情况是只跑一次,无论鼠标悬浮还是移出都不会再次启动动画。
why?因为transition和animation的工作模式是不同的,transition可以理解为监听器,一旦触发即启动。而animation则可以理解为命令,当时有效,执行完后作废,除非再下命令。
那这样是否有效呢?
div {
width: 300px;
height: 200px;
background-color: red;
-webkit-animation: test1 2s;
}
div:hover {
-webkit-animation: test1 2s;
}
@-webkit-keyframes test1 {
from {background-color: green;}
to {background-color: red;}
}
结果还是无效,但是
div {
width: 300px;
height: 200px;
background-color: red;
-webkit-animation: test1 2s;
}
div:hover {
-webkit-animation: test2 2s;
}
@-webkit-keyframes test1 {
from {background-color: green;}
to {background-color: red;}
}
@-webkit-keyframes test2 {
from {background-color: green;}
to {background-color: red;}
}
这样就有效了。
上面两个例子的区别说明了:
- animation执行完后不是作废了,而是处于end的状态
- 状态跟随animation-name,只有animation-name改变时,状态才初始化
好了,弄清楚了animation和transition最核心的区别,然后来看animation中的所有属性。用上一篇的代码作为初始代码:
div {
width: 300px;
height: 200px;
background-color: red;
-webkit-animation: test1 2s forwards;
}
div:hover {
-webkit-animation: test2 2s forwards;
}
@-webkit-keyframes test1 {
from {background-color: green;}
to {background-color: red;}
}
@-webkit-keyframes test2 {
from {background-color: red;}
to {background-color: green;}
}
animation同样是很多属性的缩写:
- animation-name: 对应keyframes的name,keyframes的定义遵循 percentage {css property...},其中from为0%,to为100%
div {
width: 300px;
height: 200px;
background-color: red;
-webkit-animation: test1 2s forwards;
}
div:hover {
-webkit-animation: test2 2s forwards;
}
@-webkit-keyframes test1 {
from {background-color: green; width: 500px;}
to {background-color: red; width: 300px;}
}
@-webkit-keyframes test2 {
from {background-color: red; width: 300px;}
to {background-color: green; width: 500px;}
}与transition表现不同的是,当动画进行的中途改变鼠标悬浮/移出的状态时,元素会先切换到最终的状态也就是新动画初始的状态再执行新动画。而不像transition一样平滑以当前的状态作为开始状态执行新动画。这是因为animation与元素当前的状态毫无关联,只按照keyframes的定义执行动画,而transition只以状态的改变触发的,所以跟元素当前的状态关联密切。
- animation-duration: 动画执行的时间。
- animation-timing-function: 动画执行的方式。
- animation-delay: 动画延迟执行的时间。
以上三个同transition,可以看出两者的设计思想是类似的,但animation无疑更复杂,因为它还有 - animation-iteration-count: number | infinite 动画执行的次数n,当然总时间就是animation-durationn * n了
div {
width: 300px;
height: 200px;
background-color: red;
-webkit-animation: test1 2s forwards 2;
}
div:hover {
-webkit-animation: test2 2s forwards 2;
}
@-webkit-keyframes test1 {
from {background-color: green;}
to {background-color: red;}
}
@-webkit-keyframes test2 {
from {background-color: red;}
to {background-color: green;}
}页面加载时元素由绿慢慢变红,然后立即变绿,最后再次慢慢由绿变红,动画进行了两次,但立即变绿很不平滑。这就要看下面这个属性
- animation-direction: normal(default) / alternate: 当执行次数为偶数时,反转关键帧进行动画
div {
width: 300px;
height: 200px;
background-color: red;
-webkit-animation: test1 2s forwards 3 alternate;
}
div:hover {
-webkit-animation: test2 2s forwards 3 alternate;
}
@-webkit-keyframes test1 {
from {background-color: green;}
to {background-color: red;}
}
@-webkit-keyframes test2 {
from {background-color: red;}
to {background-color: green;}
}元素由绿慢慢变红,然后由红慢慢变绿,最后由绿慢慢变红,动画进行了三次,而且都很平滑。
- animation-fill-mode: 定义动画启动前的delay时间内,和动画完成后的时间内元素的CSS属性。none(default) delay时间内和完成后都使用元素本来的样式,与keyframes毫无关联 | forwards delay时间内使用本来的样式,完成后使用最后一帧的样式 | backwards delay时使用第一帧的样式,完成后使用本来的样式 | both delay时间内和完成后分别使用第一帧和最后一帧的样式
div {
width: 300px;
height: 200px;
background-color: red;
/* 将none分别替换成forwards、backwards、both */
-webkit-animation: test1 2s 2s none;
}
@-webkit-keyframes test1 {
from {background-color: blue;}
to {background-color: black;}
} - animation-play-state: 规定动画暂停或运行 running(default) | paused 暂停动画。比如先定义动画,然后想在鼠标悬浮的时候再执行。
div {
width: 300px;
height: 200px;
background-color: red;
-webkit-animation: test1 2s forwards paused;
}
div:hover {
-webkit-animation-play-state: running;
}
@-webkit-keyframes test1 {
from {background-color: red;}
to {background-color: green;}
}
还有,animation emit的事件比transition多一个。
总共有三个事件:
- animationstart: 动画正式开始,要等delay的时间过去
- animationend: 动画正式结束,所有animation-iteration-count执行结束
- animationiteration: 动画重新播放时
这三个事件没有被浏览器支持,取而代之是webkit下的webkitAnimationStart、webkitAnimationEnd、webkitAnimationEnd(注意大写字母)。简单的实现如下:
(function setup() {
var e = document.getElementsByTagName("div")[0];
e.addEventListener("webkitAnimationStart", listener, false);
e.addEventListener("webkitAnimationEnd", listener, false);
e.addEventListener("webkitAnimationIteration", listener, false);
})();
function listener(e) {
var l = document.createElement("li");
switch(e.type) {
case "animationstart":
case "webkitAnimationStart":
console.log("animation start");
break;
case "animationend":
case "webkitAnimationEnd":
console.log("animation end");
break;
case "animationiteration":
case "webkitAnimationIteration":
console.log("animation restart");
break;
}
}
以上。
CSS3动画效果之animation的更多相关文章
- 鼠标悬停css3动画效果
下载Demo 效果预览 html: <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- CSS自学笔记(14):CSS3动画效果
在CSS3中也新增了一些能够对元素创建动画处理的属性.通过这些新增的属性,我们可以实现元素从一种样式变换成另一种样式时为元素添加动态效果,我们就可以取代网页中的动态图片.flash动画和JavaScr ...
- 第八十三节,CSS3动画效果
CSS3动画效果 学习要点: 1.动画简介 2.属性详解 3.简写和版本 本章主要探讨HTML5中CSS3的动画效果,可以通过类 Flash那样的关键帧模式控制运行. 一.动画简介 CSS3提 ...
- CSS3动画效果——js调用css动画属性并回调处理详解
http://www.jb51.net/css/258407.html 这篇文章主要详细介绍了CSS3动画效果回调处理,需要的朋友可以参考下 我们在做js动画的时候,很多时候都需要做回调处理,如在一个 ...
- 35个让人惊讶的CSS3动画效果
1. Pure CSS Coke Can 2. Colorful Clock 3. jQuery DJ Hero 4. Animated Pricing Column 5. Slick jQuery ...
- Css3动画效果,彩色文字效果,超简单的loveHeart
<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Cs ...
- Bounce.js – 快速创建漂亮的 CSS3 动画效果
Bounce.js 是一个用于制作漂亮的 CSS3 关键帧动画的 JavaScript 库,使用其特有的方式生成的动画效果.只需添加一个组件,选择预设,然后你就可以得到一个短网址或者导出为 CSS 代 ...
- CSS3 动画效果带来的bug
css3 动画效果比如transition:all 2s linear;这种用来计算及时的物体坐标的话会带来一定的问题 比如把一个DIV从A点移动到B点.JS为DIV.style.left=B; 但是 ...
- animate.css 一些常用的CSS3动画效果
大家已经开始在项目中使用一些CSS3动画效果了吧,这让网站在高端浏览器上看起来很上流.animate.css是一个老外做的各种CSS3动画的合集,比较全,也很炫,大家可以参考学习一下. 项目主页:ht ...
随机推荐
- Linux入门基础知识
注:内容系兄弟连Linux教程(百度传课:史上最牛的Linux视频教程)的学习笔记. Linux入门基础知识 1. Unix和Linux发展历史 二者就像父子关系,当然Unix是老爹.1965年,MI ...
- java中什么样的对象能够进入老年代
1.大对象:所谓的大对象是指需要大量连续内存空间的java对象,最典型的大对象就是那种很长的字符串以及数组,大对象对虚拟机的内存分配就是坏消息,尤其是一些朝生夕灭的短命大对象,写程序时应避免. 2.长 ...
- phpstudy命令行中数据表插入中文显示不了的问题
在PHPstudy环境下,做MySQL操作,往数据表里面插入数据的时候,如果是中文的数据就会显示不了或者是问号?. 这个问题搞了我一晚上了,终于知道问题所在. 下载的PHPstudy的MySQL数据库 ...
- git for windows+TortoiseGit客户端的使用二
通常都是使用git协议方式来连接服务器,然后使用https方式的连接方法,是如何设置的: 先登录github服务器,获取远程服务器仓库: 在本地创建一个存放仓库的目录,然后使用tortoiseGit客 ...
- 照着官方文档学习react
笨人学习法 10000个小时策略来学习,因为笨.先照着官方文档敲一遍,写一遍. 准备 先要准备环境.搭建一个基于webpack的react环境:Hello ReactJS. demo: https:/ ...
- selenium+java破解极验滑动验证码
摘要 分析验证码素材图片混淆原理,并采用selenium模拟人拖动滑块过程,进而破解验证码. 人工验证的过程 打开威锋网注册页面(https://passport.feng.com/?r=user/r ...
- xhost和DISPLAY
xhost是用来控制X server访问权限的. 通常当你从hostA登陆到hostB上运行hostB上的应用程序时,做为应用程序来说,hostA是client,但是作为图形来说,是在hostA上显示 ...
- 【环境搭建】使用Jekyll搭建Github博客
前言 昨天花了差不多一天的时间,使用Jekyll搭建起了一套Github博客,感觉不错,也特将搭建过程记录下来,方便有需要的朋友自行搭建. 搭建步骤 本环境是在Linux环境下搭建完成的 安装前建议使 ...
- Lightoj1205——Palindromic Numbers(数位dp+回文数)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- iOS绘图框架CoreGraphics分析
由于CoreGraphics框架有太多的API,对于初次接触或者对该框架不是十分了解的人,在绘图时,对API的选择会感到有些迷茫,甚至会觉得iOS的图形绘制有些繁琐.因此,本文主要介绍一下iOS的绘图 ...