分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

CSS3动画(重要)

发布时间:2023-09-06 01:08责任编辑:熊小新关键词:CSS动画

CSS3 动画

CSS3,我们可以创建动画,它可以取代许多网页动画图像,Flash动画,和JAVAScripts。

CSS3 @keyframes 规则

要创建CSS3动画,你将不得不了解@keyframes规则。

@keyframes规则是创建动画。 @keyframes规则内指定一个CSS样式和动画将逐步从目前的样式更改为新的样式。

实例:

@keyframes myfirst{ ???from {background: red;} ???to {background: yellow;}} @-webkit-keyframes myfirst /* Safari 与 Chrome */{ ???from {background: red;} ???to {background: yellow;}}

CSS3 动画

当在 @keyframes 创建动画,把它绑定到一个选择器,否则动画不会有任何效果。

指定至少这两个CSS3的动画属性绑定向一个选择器:

  • 规定动画的名称
  • 规定动画的时长

例子:  把 "myfirst" 动画捆绑到 div 元素,时长:5 秒:

<!DOCTYPE html><html><head><meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <style> div{ ???width:100px; ???height:100px; ???background:red; ???animation:myfirst 5s; ???-webkit-animation:myfirst 5s; /* Safari and Chrome */}@keyframes myfirst{ ???from {background:red;} ???to {background:yellow;}}@-webkit-keyframes myfirst /* Safari and Chrome */{ ???from {background:red;} ???to {background:yellow;}}</style></head><body><p><b>注意:</b> 该实例在 Internet Explorer 9 及更早 IE 版本是无效的。</p><div></div></body></html>

注意: 您必须定义动画的名称和动画的持续时间。如果省略的持续时间,动画将无法运行,因为默认值是0。

CSS3动画是什么?

动画是使元素从一种样式逐渐变化为另一种样式的效果。

您可以改变任意多的样式任意多的次数。

请用百分比来规定变化发生的时间,或用关键词 "from" 和 "to",等同于 0% 和 100%。

0% 是动画的开始,100% 是动画的完成。

为了得到最佳的浏览器支持,您应该始终定义 0% 和 100% 选择器。

例子:  当动画为 25% 及 50% 时改变背景色,然后当动画 100% 完成时再次改变:

<!DOCTYPE html><html><head><meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <style> div{ ???width:100px; ???height:100px; ???background:red; ???animation:myfirst 5s; ???-moz-animation:myfirst 5s; /* Firefox */ ???-webkit-animation:myfirst 5s; /* Safari and Chrome */ ???-o-animation:myfirst 5s; /* Opera */}@keyframes myfirst{ ???0% ??{background:red;} ???25% ?{background:yellow;} ???50% ?{background:blue;} ???100% {background:green;}}@-moz-keyframes myfirst /* Firefox */{ ???0% ??{background:red;} ???25% ?{background:yellow;} ???50% ?{background:blue;} ???100% {background:green;}}@-webkit-keyframes myfirst /* Safari and Chrome */{ ???0% ??{background:red;} ???25% ?{background:yellow;} ???50% ?{background:blue;} ???100% {background:green;}}@-o-keyframes myfirst /* Opera */{ ???0% ??{background:red;} ???25% ?{background:yellow;} ???50% ?{background:blue;} ???100% {background:green;}}</style></head><body><div></div><p><b>注释:</b>当动画完成时,会变回初始的样式。</p><p><b>注意:</b> 该实例在 Internet Explorer 9 及更早 IE 版本是无效的。</p></body></html>

改变背景色和位置:

<!DOCTYPE html><html><head><meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <style> div{ ???width:100px; ???height:100px; ???background:red; ???position:relative; ???animation:myfirst 5s; ???-webkit-animation:myfirst 5s; /* Safari and Chrome */}@keyframes myfirst{ ???0% ??{background:red; left:0px; top:0px;} ???25% ?{background:yellow; left:200px; top:0px;} ???50% ?{background:blue; left:200px; top:200px;} ???75% ?{background:green; left:0px; top:200px;} ???100% {background:red; left:0px; top:0px;}}@-webkit-keyframes myfirst /* Safari and Chrome */{ ???0% ??{background:red; left:0px; top:0px;} ???25% ?{background:yellow; left:200px; top:0px;} ???50% ?{background:blue; left:200px; top:200px;} ???75% ?{background:green; left:0px; top:200px;} ???100% {background:red; left:0px; top:0px;}}</style></head><body><p><b>注意:</b> 该实例在 Internet Explorer 9 及更早 IE 版本是无效的。</p><div></div></body></html>

运行myfirst动画,设置所有的属性:(用法一)  先定义动画,然后绑定到元素。

<!DOCTYPE html><html><head><meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title><style> div{ ???width:100px; ???height:100px; ???background:red; ???position:relative; ???animation-name:myfirst; ???animation-duration:5s; ???animation-timing-function:linear; ???animation-delay:2s; ???animation-iteration-count:infinite; ???animation-direction:alternate; ???animation-play-state:running; ???/* Safari and Chrome: */ ???-webkit-animation-name:myfirst; ???-webkit-animation-duration:5s; ???-webkit-animation-timing-function:linear; ???-webkit-animation-delay:2s; ???-webkit-animation-iteration-count:infinite; ???-webkit-animation-direction:alternate; ???-webkit-animation-play-state:running;}@keyframes myfirst{ ???0% ??{background:red; left:0px; top:0px;} ???25% ?{background:yellow; left:200px; top:0px;} ???50% ?{background:blue; left:200px; top:200px;} ???75% ?{background:green; left:0px; top:200px;} ???100% {background:red; left:0px; top:0px;}}@-webkit-keyframes myfirst /* Safari and Chrome */{ ???0% ??{background:red; left:0px; top:0px;} ???25% ?{background:yellow; left:200px; top:0px;} ???50% ?{background:blue; left:200px; top:200px;} ???75% ?{background:green; left:0px; top:200px;} ???100% {background:red; left:0px; top:0px;}}</style></head><body><p><b>注意:</b> 该实例在 Internet Explorer 9 及更早 IE 版本是无效的。</p><div></div></body></html>

与上面的动画相同,但是使用了简写的动画 animation 属性:(常用)

<!DOCTYPE html><html><head><meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title><style> div{ ???width:100px; ???height:100px; ???background:red; ???position:relative; ???animation:myfirst 5s linear 2s infinite alternate; ???/* Firefox: */ ???-moz-animation:myfirst 5s linear 2s infinite alternate; ???/* Safari and Chrome: */ ???-webkit-animation:myfirst 5s linear 2s infinite alternate; ???/* Opera: */ ???-o-animation:myfirst 5s linear 2s infinite alternate;}@keyframes myfirst{ ???0% ??{background:red; left:0px; top:0px;} ???25% ?{background:yellow; left:200px; top:0px;} ???50% ?{background:blue; left:200px; top:200px;} ???75% ?{background:green; left:0px; top:200px;} ???100% {background:red; left:0px; top:0px;}}@-moz-keyframes myfirst /* Firefox */{ ???0% ??{background:red; left:0px; top:0px;} ???25% ?{background:yellow; left:200px; top:0px;} ???50% ?{background:blue; left:200px; top:200px;} ???75% ?{background:green; left:0px; top:200px;} ???100% {background:red; left:0px; top:0px;}}@-webkit-keyframes myfirst /* Safari and Chrome */{ ???0% ??{background:red; left:0px; top:0px;} ???25% ?{background:yellow; left:200px; top:0px;} ???50% ?{background:blue; left:200px; top:200px;} ???75% ?{background:green; left:0px; top:200px;} ???100% {background:red; left:0px; top:0px;}}@-o-keyframes myfirst /* Opera */{ ???0% ??{background:red; left:0px; top:0px;} ???25% ?{background:yellow; left:200px; top:0px;} ???50% ?{background:blue; left:200px; top:200px;} ???75% ?{background:green; left:0px; top:200px;} ???100% {background:red; left:0px; top:0px;}}</style></head><body><p><b>注意:</b> 该实例在 Internet Explorer 9 及更早 IE 版本是无效的。</p><div></div></body></html>

CSS3动画(重要)

原文地址:http://www.cnblogs.com/qlqwjy/p/7466820.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved