CSS样式更改——过渡、动画

简介: CSS样式更改——过渡、动画

前言


上篇文章主要讲述了CSS样式更改中的2D转换,这篇文章我们来介绍下CSS样式更改中的过渡、动画基础用法。


1.过渡

元素从一种样式逐渐改变为另一种的样式

div{transition: width 1s;-moz-transition: width 1s;  /* Firefox 4 */-webkit-transition: width 1s;  /* Safari 和 Chrome */-o-transition: width 1s;  /* Opera */}transition-property:应用过渡的Css属性的名称 比如宽度widthtransition-duration:过渡效果花费的时间   比如1stransition-timing-function:渡效果的时间曲线 如下所示:linear 匀速ease 先慢后快ease-in 慢速开始ease-out 慢速结束ease-in-out 慢速开始和结束cubic-bezier(n,n,n,n) 在cubic-bezie 函数中定义自己的值,可能的值是0至1之间的数值transition-delay:过渡效果何时开始 如1s


2.动画 Animation

1).首先定义@keyframes 规则
@keyframes my{from {background: red;}to {background: yellow;}}
@-moz-keyframes my /* Firefox */{from {background: red;}to {background: yellow;}}
@-webkit-keyframes my /* Safari 和 Chrome */{from {background: red;}to {background: yellow;}}
@-o-keyframes my /* Opera */{from {background: red;}to {background: yellow;}}

为了丰富元素的变化过程,你可以把from  to改为百分比的样子:

@keyframes my{0%   {background: red;}25%  {background: yellow;}50%  {background: blue;}100% {background: green;}}
@-moz-keyframes my /* Firefox */{0%   {background: red;}25%  {background: yellow;}50%  {background: blue;}100% {background: green;}}
@-webkit-keyframes my /* Safari 和 Chrome */{0%   {background: red;}25%  {background: yellow;}50%  {background: blue;}100% {background: green;}}
@-o-keyframes my /* Opera */{0%   {background: red;}25%  {background: yellow;}50%  {background: blue;}100% {background: green;}}

定义好了,接下来我们就可以启动我们的动画了。

2).animation启动动画效果
div{animation-name: my;animation-duration: 5s;animation-timing-function: linear;animation-delay: 2s;animation-iteration-count: infinite;animation-direction: alternate;animation-play-state: running;/* Firefox: */-moz-animation-name: my;-moz-animation-duration: 5s;-moz-animation-timing-function: linear;-moz-animation-delay: 2s;-moz-animation-iteration-count: infinite;-moz-animation-direction: alternate;-moz-animation-play-state: running;/* Safari 和 Chrome: */-webkit-animation-name: my;-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;/* Opera: */-o-animation-name: my;-o-animation-duration: 5s;-o-animation-timing-function: linear;-o-animation-delay: 2s;-o-animation-iteration-count: infinite;-o-animation-direction: alternate;-o-animation-play-state: running;}
animation-name                   选择器的 keyframes 的名称animation-duration               动画所花费的时间animation-timing-function        匀速播放动画animation-delay           动画过多久开始animation-iteration-count        播放动画次数animation-direction       是否在下一周期逆向地播放 normal 正常播放  alternate 轮流反向播放animation-play-state             暂停动画  paused 动画已暂停  running 动画正在播放animation-fill-modenone         不填充forwards     当动画完成后,保持最后一个属性值backwards     在animation-delay 所指定的一段时间内,在动画显示之前,应用开始属性值both        向前和向后填充模式都被应用。

参考文档:W3C官方文档(CSS篇)


总结


这篇文章主要介绍了CSS样式更改篇中的过度和动漫基础知识,希望让大家对CSS样式更改有个简单的认识和了解。

相关文章
|
7月前
|
前端开发 JavaScript
CSS 过渡和动画
CSS过渡和动画是用于为网页元素添加动态效果的两种重要技术
248 73
|
5月前
|
前端开发
|
8月前
css3 svg制作404页面动画效果HTML源码
css3 svg制作404页面动画效果HTML源码
142 34
|
8月前
jQuery+CSS3实现404背景游戏动画源码
jQuery+CSS3实现404背景游戏动画源码
114 22
|
10月前
纯css3实现的百分比渐变进度条加载动画源码
纯css3实现的百分比渐变进度条加载动画特效源码
176 31
|
9月前
|
Web App开发 移动开发 JavaScript
纯CSS3+SVG实现的节日庆祝五彩纸屑动画效果源码
这是一款基于纯CSS3+SVG实现的节日庆祝五彩纸屑动画效果源码。画面中左下角是一个圆锥形礼炮卡通效果,呈现出节日庆祝时礼花爆破、五彩纸屑纷飞的动画特效。整体动画效果采用纯css3+svg实现,没有引入任何外部图形或js脚本元素。建议使用支持HTML5与css3效果较好的火狐(Firefox)或谷歌(Chrome)等浏览器预览本源码。
176 6
|
9月前
|
JavaScript 前端开发
jQuery和CSS3斑马线样式range滑块特效
jQuery和CSS3斑马线样式range滑块特效
|
10月前
|
前端开发 JavaScript UED
CSS滚动效果和视差滚动的原理、应用及其对用户体验的影响。从平滑滚动到元素跟随,再到滚动触发动画
本文探讨了CSS滚动效果和视差滚动的原理、应用及其对用户体验的影响。从平滑滚动到元素跟随,再到滚动触发动画,这些效果增强了页面的吸引力和互动性。视差滚动通过不同层次元素的差异化移动,增加了页面的深度感和沉浸感。文章还讨论了实现方法、性能优化及案例分析,旨在为设计师和开发者提供实用指导。
238 7
|
10月前
纯css3加载loading发光变色动画代码
纯css3加载loading发光变色动画特效代码是一款基于css3 keyframes属性实现的发光变色圆点串联旋转loading加载动画
114 2