CSS Transition (变换动画)

本文分享了学习CSS时发现的有用内容,主要介绍了CSS 3中的Transition属性,它能在元素属性数值改变时产生过渡效果,如颜色渐变、拉伸伸缩、位置移动等。还提及了浏览器支持情况、属性由来、书写方法,以及多种变换的添加和计时延迟等。

最近学习CSS的过程中,发现了不少有用的东西。包括一些神奇的,纯CSS的特效,只需简单的几笔即可完成jQuery实现的效果。

CSS 3 中提供了一种属性,Transition(变换),这种属性能够实现在元素的某些属性的数值发生改变时产生过渡的效果。比如长度增加,能产生类似拉长的动画效果;颜色改变时,也可以利用Transition产生一种颜色渐变的效果。

  • 转载原链接地址:http://blog.netsh.org/posts/css-transition-animate-tutorial_522.netsh.html

浏览器支持情况

CSS Transition受到浏览器的广泛支持。

Chrome 2+(-webkit-transition)
Firefox3.7+(-moz-transition)
Safari 3.1+(-webkit-transition)
Opera 10.5+(-o-transition)

From:axiu.me

不过经过我的观察,现在IE还是不能支持,即使是在IE9中。不过也没有关系,至少并不会出现什么令人感觉糟糕的结果。

CSS变换的由来

CSS Transition曾经是只属于Apple Safari Webkit的东西,仅能由Safari UI实现的动画效果。

可是W3C有部分工作人员认为变换动画是脚本该做的事情而不是CSS,不过去年三月份,来自Apple、Mozilla开始将CSS变换模块添加到CSS 3特性里面,非常接近原来Safari Webkit的效果。由此得来CSS Transition。

书写方法

在CSS代码中,要使用Transition属性需要这么书写:

-moz-transition: // Firefox

-webkit-transition: // Safari、Chrome

-o-transition: // Opera

transition: //官方标准

建议在书写时,将上述全写上。

效果之:颜色变换

使用Transition可以实现颜色的变换,比如一个锚链接的鼠标移上产生颜色渐变动画:

其核心代码如下:

a:hover {
 color: red;
 background-color: rgb(255,204,255);
 -webkit-transition: color .5s linear, background-color .5s linear; 
 transition: color .5s linear, background-color .5s linear; 
}

效果之:拉伸、伸缩效果


其核心代码如下:

#example2 {
	height:200px; 
}
#example2 a:link {
color: blue;
text-decoration: none;
-webkit-transition: color .25s ease-in 0s;
transition: color .25s ease-out 0s; 
}
#example2 a:hover {
color: red;
-webkit-transition: color .25s ease-in .1s;
transition: color .25s ease-out .1s; 
}
#example2 a:active {
color: green;
transition: color .25s ease; 
}
#example2 ul {
list-style: none;
margin: 0;
padding: 0;
}
#example2 .menu {
display: block;
position: relative;
top: .9em;
left: 0;
padding: 10px;
height: auto;
width: 100px;
border: 8px solid rgba(204,204,204,.5);
cursor: pointer;
background-color: rgba(255,255,255,.75);
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
#example2 ul.menu  li {
font-weight: normal;
list-style: none;
margin:0
}
#example2 ul.menu  li a:link {
font-weight: normal;
list-style: none;
font-size: 12px;
margin-left: 0;
padding-left: 0;
}
#example2 ul.menu ul li {
font-weight: normal;
padding: 5px 0;
margin:0;
border-top: 1px solid rgb(204,204,204);
background-color: rgb(255,255,255);
-webkit-transition: background-color .5s ease; 
transition: background-color .2s ease; 
}
#example2 .drop {
display: block;
position: relative;
height: 0;
overflow: hidden;
width: 100px;
opacity: 0;
-webkit-transition: opacity .25s linear 0s, height .25s ease-out .1s;
transition: opacity .25s linear 0s, height .25s ease-out .1s;  
}
#example2 ul.menu ul li:hover {
background-color: rgb(234,234,234);
-webkit-transition: background-color .5s ease; 
transition: background-color .2s ease; 
}
#example2 ul.menu:hover .drop {
height: 140px;
opacity: 1;
-webkit-transition: opacity .25s linear 0s,  height .25s linear 0s;
transition: opacity .25s linear 0s,  height .25s linear 0s; 
}

效果之:位置移动

你可以试想,如果在发生位置之间的改变时,如果使用Transition属性,则可以做到过渡移动的动画效果,这非常像是脚本做出的事情。

位置移动

按住鼠标不放

其核心代码如下:

#example3 {
	background-color: black;
	color: white;
}
#example3 .control {
	text-align: center; font-size: 3em;
}
#example3 .move { cursor: pointer;}
#example3 .move>#astro {
	position: relative;
	top: 0;
	left: 250px;
	-webkit-transition: top 2s ease-in-out, left 2s ease;
	transition: top 2s  ease-in-out, left 2s ease;
}
#example3 .move:active>#astro {
	top: 10px;
	left: 10px;
	-webkit-transition: top 2s ease-in-out, left 2s ease;
	transition: top 2s  ease-in-out, left 2s ease;
}

补充

同时添加多种变换

如果你希望你的元素的颜色、背景都发生渐变,例如:

a:hover {
  color: red;
  background-color: pink;
  -webkit-transition: color .25s linear;
  transition: color .25s linear;
  -webkit-transition: background-color .15s linear .1;
  transition: background-color .15s linear .1;
}

这并不能达到目的,后面的将会覆盖掉前面的,因此方法是使用逗号同时使用多种变换效果:

a:hover {
  color: red;
  background-color: pink;
   -webkit-transition: color .25s linear, background-color .15s linear .1s;
  transition: color .25s linear, background-color .15s linear .1s;
 }

变换计时与延迟

如果你希望自定义你的变换效果,比如你希望自定义动画改变的速率,CSS提供了如下关键字可以添加在位尾后:

名称如何工作
cubic-bezier(x1, y1, x2, y2)X 和 Y 值在0到1之间,以定义用于Time function的贝塞尔曲线的形状。
linear均速
ease逐渐慢下来
ease-in加速(渐入)
ease-out减速(淡出)
ease-in-out加速然后减速

附录:可以发生变换的属性

那些属性可以变换呢?几乎除了box-shadow以外,其他的都可以发生变换。

CSS属性改变的对象
background-color色彩
background-image只是渐变
background-position百分比,长度
border-bottom-color色彩
border-bottom-width长度
border-color色彩
border-left-color色彩
border-left-width长度
border-right-color色彩
border-right-width长度
border-spacing长度
border-top-color色彩
border-top-width长度
border-width长度
bottom百分比,长度
color色彩
crop百分比
font-size百分比,长度
font-weight数字
grid-*数量
height百分比,长度
left百分比,长度
letter-spacing长度
line-height百分比,长度,数字
margin-bottom长度
margin-left长度
margin-right长度
margin-top长度
max-height百分比,长度
max-width百分比,长度
min-height百分比,长度
min-width百分比,长度
opacity数字
outline-color色彩
outline-offset整数
outline-width长度
padding-bottom长度
padding-left长度
padding-right长度
padding-top长度
right百分比,长度
text-indent百分比,长度
text-shadow阴影
top百分比,长度
vertical-align百分比,长度,关键词
visibility可见性
width百分比,长度
word-spacing百分比,长度
z-index正整数
zoom数字

谢谢收看。

转载于:https://www.cnblogs.com/xiaoky/p/3949154.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值