
我们可以使用 CSS 添加过渡到 HTML 元素。在开始本教程之前,让我们先了解一下什么是过渡。基本上,转换是元素从一种状态变为另一种状态。例如,当用户将鼠标悬停在元素上时,我们会更改元素的尺寸。
在 CSS 中,我们可以使用两种方式向元素添加过渡。首先是同时使用“transition-property”、“transition-duration”、“transition-timing-function”和“transition-delay”这四个属性。第二种是仅使用“transition”CSS 属性。
CSS“transition”属性是以下 CSS 属性的简写。
Transition-property - 它指定我们需要应用过渡效果的 CSS 属性。如果我们需要对所有属性应用转换,我们可以使用“all”作为值。
立即学习“前端免费学习笔记(深入)”;
Transition-duration - 过渡效果的总时间(以秒为单位)。
Transition-timing-function - 它确定转换应如何进行。过渡计时函数的示例有“liner”、“ease-in”、“ease-out”、“ease-in-out”等。
Transition-delay - 这是过渡效果开始后的一段时间。
用户可以按照以下语法将过渡简写与多个 CSS 属性一起使用。
element {
transition: Property duration timing-function delay;
}
在上面的语法中,第一个值是转换属性,第二个值是转换持续时间,第三个值是计时函数,最后一个值是转换延迟。
在下面的示例中,我们有一个尺寸为 200 x 200 的 div 元素,并且我们在 div 元素的高度上添加了持续 2 秒的过渡效果。这里,转换延迟为0.5秒,计时功能为“ease-in-out”。
用户可以将鼠标悬停在 div 元素上来观察过渡效果。我们可以观察到 div 元素的高度是平滑增加的,而不是立即增加的。
<html>
<head>
<style>
/* adding transition effect on the element */
.element {
width: 500px;
height: 200px;
background-color: red;
color: white;
font-size: 25px;
transition: height 2s ease-in-out 0.5s;
}
.element:hover {
height: 400px;
background-color: green;
}
</style>
</head>
<body>
<h3>Using the <i> transition property </i> of CSS to add transition to the element</h3>
<div class = "element">
This div contains the texts.
<p> Hover over this div and wait to see the changes </p>
</div>
</body>
</html>
在下面的示例中,div 元素的初始 margin-left 为 2px。当用户将鼠标悬停在 div 元素上时,我们将 margin-left 增加到 100px。我们在 div 元素的 margin-left CSS 属性上添加了延迟 0.5 秒后持续 2 秒的过渡效果。
在输出中,将鼠标悬停在 div 元素上,该元素将在 2 秒内向右移动 100px。
<html>
<head>
<style>
/* adding transition effect on the element */
.element {
width: 200px;
height: 200px;
background-color: blue;
color: white;
font-size: 25px;
margin-left: 2px;
border-radius: 12px;
transition: margin-left 2s ease-in-out 0.5s;
}
.element:hover {
margin-left: 100px;
}
</style>
</head>
<body>
<h3> Using the <i> transition property </i> of CSS to add transition to the element </h3>
<p> Hover over the below div and wait to see the changes. </p>
<div class = "element">
This div contains the texts.
</div>
</body>
</html>
在下面的示例中,我们为多个 CSS 属性添加了过渡效果。在这里,我们为“margin-left”、“height”、“width”、“background-color”、“color”和“font-size”CSS属性添加了2秒的过渡效果。
在输出中,用户可以观察到所有 CSS 属性的过渡都很平滑。但是,我们可以使用“all”作为“transition-property”的值来向所有属性添加过渡。
<html>
<head>
<style>
/* adding transition effect on the element */
.element {
width: 200px;
height: 200px;
background-color: blue;
color: white;
font-size: 25px;
margin-left: 2px;
border-radius: 12px;
transition: margin-left 2s, height 2s, width 2s, background-color 2s, color 2s, font-size 2s;
}
.element:hover {
margin-left: 100px;
height: 400px;
width: 400px;
background-color: aqua;
color: black;
font-size: 40px;
}
</style>
</head>
<body>
<h3> Using the <i> transition property </i> of CSS to add transition to the element </h3>
<p> Hover over the bellow div to see the achennges</p>
<div class = "element">
Square div element.
</div>
</body>
</html>
用户学会了使用“transition”CSS 属性,这是与过渡相关的多个 CSS 属性的简写。在这里,我们已经学会了在上面的三个示例中使用“transition”CSS属性。在上一个示例中,我们为多个 CSS 属性添加了过渡效果。
以上就是CSS 中具有多个属性的过渡简写?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号