CSS3 过渡是元素从一种样式逐渐改变为另一种的效果。
要实现这一点,必须规定两项内容:
- 指定要添加效果的CSS属性
- 指定效果的持续时间
语法
下表列出了所有的过渡属性:
属性 | 描述 | CSS |
---|---|---|
transition | 简写属性,用于在一个属性中设置四个过渡属性。 | 3 |
transition-property | 规定应用过渡的 CSS 属性的名称。 | 3 |
transition-duration | 定义过渡效果花费的时间。默认是 0。 | 3 |
transition-timing-function | 规定过渡效果的时间曲线。默认是 “ease”。 | 3 |
transition-delay | 规定过渡效果何时开始。默认是 0。 | 3 |
简写
<div class="box"></div>
<style>
.box {
margin: 50px auto;
width: 200px;
height: 200px;
background-color: orange;
// 过渡属性、过渡效果花费时间、过渡效果时间曲线、过渡延迟开始时间
transition: background-color 0.5s linear 1s;
}
.box:hover {
background-color: green;
}
</style>
过渡的css属性 & 过渡花费时间
<div class="box"></div>
<style>
.box {
margin: 50px auto;
width: 200px;
height: 200px;
background-color: olivedrab;
opacity: 0.4;
/* 过渡的css属性,可以写多个,也可以写 all */
transition-property: background-color, opacity;
/* 过渡花费时间 */
transition-duration: 0.5s
}
.box:hover {
background-color: green;
opacity: 1;
}
</style>
过渡时间曲线
transition-timing-function: linear|ease|ease-in|ease-out|ease-in-out|cubic-bezier(n,n,n,n);
值 | 描述 |
---|---|
linear | 规定以相同速度开始至结束的过渡效果(等于 cubic-bezier(0,0,1,1))。 |
ease | 默认值,规定慢速开始,然后变快,然后慢速结束的过渡效果(cubic-bezier(0.25,0.1,0.25,1))。 |
ease-in | 规定以慢速开始的过渡效果(等于 cubic-bezier(0.42,0,1,1))。 |
ease-out | 规定以慢速结束的过渡效果(等于 cubic-bezier(0,0,0.58,1))。 |
ease-in-out | 规定以慢速开始和结束的过渡效果(等于 cubic-bezier(0.42,0,0.58,1))。 |
cubic-bezier(n,n,n,n) | 在 cubic-bezier 函数中定义自己的值。可能的值是 0 至 1 之间的数值。 |
完整体验时间曲线效果
过渡延迟开始时间
<div class="box"></div>
<style>
.box {
margin: 50px auto;
width: 200px;
height: 200px;
background-color: olivedrab;
opacity: 0.4;
/* 过渡的css属性 */
transition-property: background-color;
/* 过渡花费时间 */
transition-duration: 0.5s;
/* 过渡延迟开始时间 */
transition-delay: 1s
}
.box:hover {
background-color: green;
opacity: 1;
}
</style>
transition + transform
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>css3 transition 与 transform</title>
<style>
.box {
width: 100px;
background: #92B901;
color: #ffffff;
position: absolute;
font-weight: bold;
font-size: 16px;
padding: 10px;
float: left;
margin: 5px;
transition-property: width, height, transform, background, opacity;
/* 与 过度的css属性一一对应 */
transition-duration: 1s, 1s, 1s, 1s, 1s;
border-radius: 5px;
opacity: 0.4;
}
.box:hover {
background-color: orange;
opacity: 1;
transform: translate(50px, 50px) scale(1.5) rotate(720deg) ;
}
</style>
</head>
<body>
<div class="box">css3 transition 与 transform</div>
</body>
</html>