面试题答案
一键面试CSS过渡(transition)属性基本语法
transition: property duration timing-function delay;
常用属性值
- property:指定应用过渡效果的 CSS 属性名称,如
width
、height
、background - color
等,可使用all
表示所有属性。 - duration:过渡效果持续的时间,单位为秒(s)或毫秒(ms)。
- timing - function:规定过渡效果的速度曲线,常见值有
ease
(默认,慢 - 快 - 慢)、linear
(匀速)、ease - in
(慢开始)、ease - out
(慢结束)、ease - in - out
(慢开始和慢结束)。 - delay:过渡效果开始前的延迟时间,单位为秒(s)或毫秒(ms)。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<style>
button {
padding: 10px 20px;
background-color: blue;
color: white;
border: none;
border - radius: 5px;
transition: background-color 0.5s ease - in - out, border - radius 0.5s ease - in - out;
}
button:hover {
background-color: red;
border - radius: 15px;
}
</style>
</head>
<body>
<button>悬停我</button>
</body>
</html>
上述代码中,当鼠标悬停在按钮上时,按钮的背景颜色和边框半径会在 0.5 秒内以 ease - in - out
的速度曲线进行过渡变化。