CSS 过渡允许您在给定的持续时间内平滑地更改属性值。
将鼠标悬停在下面的元素上即可查看 CSS 过渡效果:
在本章中,您将了解以下属性:
transition
transition-delay
transition-duration
transition-property
transition-timing-function
表中的数字指定完全支持该属性的第一个浏览器版本。
Property | |||||
---|---|---|---|---|---|
transition | 26.0 | 10.0 | 16.0 | 6.1 | 12.1 |
transition-delay | 26.0 | 10.0 | 16.0 | 6.1 | 12.1 |
transition-duration | 26.0 | 10.0 | 16.0 | 6.1 | 12.1 |
transition-property | 26.0 | 10.0 | 16.0 | 6.1 | 12.1 |
transition-timing-function | 26.0 | 10.0 | 16.0 | 6.1 | 12.1 |
要创建过渡效果,您必须指定两件事:
您想要添加效果的 CSS 属性
效果的持续时间
注意:如果未指定持续时间部分,则过渡将不起作用,因为默认值为 0。
以下示例显示了一个 100px * 100px 红色 <div> 元素。 <div> 元素还为 width 属性指定了过渡效果,持续时间为 2 秒:
div
{
width: 100px;
height: 100px;
background: red;
transition: width 2s;
}
当指定的CSS属性(宽度)改变值时,过渡效果将开始。
现在,当用户将鼠标悬停在 <div> 元素上时,让我们为 width 属性指定一个新值:
div:hover
{
width: 300px;
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s;
}
div:hover {
width: 300px;
}
</style>
</head>
<body>
<h1>The transition Property</h1>
<p>Hover over the div element below, to see the transition effect:</p>
<div></div>
</body>
</html>
请注意,当光标移出元素时,它会逐渐变回原来的样式。
下面的示例为 width 和 height 属性添加了过渡效果,并具有持续时间 宽度为 2 秒,高度为 4 秒:
div
{
transition: width 2s, height 4s;
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s, height 4s;
}
div:hover {
width: 300px;
height: 300px;
}
</style>
</head>
<body>
<h1>The transition Property</h1>
<p>Hover over the div element below, to see the transition effect:</p>
<div></div>
</body>
</html>
transition-timing-function
属性指定过渡效果的速度曲线。
转换计时函数属性可以具有以下值:
ease
- 指定一个缓慢开始,然后快速,然后缓慢结束的过渡效果(这是默认值)
linear
- 指定从开始到结束具有相同速度的过渡效果
ease-in
- 指定缓慢启动的过渡效果
ease-out
- 指定缓慢结束的过渡效果
ease-in-out
- 指定缓慢开始和结束的过渡效果
cubic-bezier(n,n,n,n)
- 让您在三次贝塞尔函数中定义自己的值
以下示例显示了一些可以使用的不同速度曲线:
#div1 {transition-timing-function: linear;}
#div2
{transition-timing-function: ease;}
#div3 {transition-timing-function:
ease-in;}
#div4 {transition-timing-function: ease-out;}
#div5
{transition-timing-function: ease-in-out;}
自己尝试一下 →
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s;
}
#div1 {transition-timing-function: linear;}
#div2 {transition-timing-function: ease;}
#div3 {transition-timing-function: ease-in;}
#div4 {transition-timing-function: ease-out;}
#div5 {transition-timing-function: ease-in-out;}
div:hover {
width: 300px;
}
</style>
</head>
<body>
<h1>The transition-timing-function Property</h1>
<p>Hover over the div elements below, to see the different speed curves:</p>
<div id="div1">linear</div><br>
<div id="div2">ease</div><br>
<div id="div3">ease-in</div><br>
<div id="div4">ease-out</div><br>
<div id="div5">ease-in-out</div><br>
</body>
</html>
transition-delay
属性指定过渡效果的延迟(以秒为单位)。
以下示例在开始前有 1 秒的延迟:
div {
transition-delay: 1s;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition: width 3s;
transition-delay: 1s;
}
div:hover {
width: 300px;
}
</style>
</head>
<body>
<h1>The transition-delay Property</h1>
<p>Hover over the div element below, to see the transition effect:</p>
<div></div>
<p><b>Note:</b> The transition effect has a 1 second delay before starting.</p>
</body>
</html>
以下示例为转换添加过渡效果:
div {
transition:
width 2s, height 2s, transform 2s;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s, height 2s, transform 2s;
}
div:hover {
width: 300px;
height: 300px;
transform: rotate(180deg);
}
</style>
</head>
<body>
<h1>Transition + Transform</h1>
<p>Hover over the div element below:</p>
<div></div>
</body>
</html>
CSS 过渡属性可以一一指定,如下所示:
div
{
transition-property: width;
transition-duration: 2s;
transition-timing-function: linear;
transition-delay: 1s;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition-property: width;
transition-duration: 2s;
transition-timing-function: linear;
transition-delay: 1s;
}
div:hover {
width: 300px;
}
</style>
</head>
<body>
<h1>The transition Properties Specified One by One</h1>
<p>Hover over the div element below, to see the transition effect:</p>
<div></div>
<p><b>Note:</b> The transition effect has a 1 second delay before starting.</p>
</body>
</html>
或者使用简写属性 transition
:
div
{
transition: width 2s linear 1s;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s linear 1s;
}
div:hover {
width: 300px;
}
</style>
</head>
<body>
<h1>Using The transition Shorthand Property</h1>
<p>Hover over the div element below, to see the transition effect:</p>
<div></div>
<p><b>Note:</b> The transition effect has a 1 second delay before starting.</p>
</body>
</html>
下表列出了所有 CSS 过渡属性:
用于将四个转换属性设置为单个属性的简写属性
指定过渡效果的延迟(以秒为单位)
指定过渡效果需要多少秒或毫秒才能完成
指定过渡效果所针对的 CSS 属性的名称
指定过渡效果的速度曲线