在CSS中,可以使用transition
屬性為hover狀態添加動畫過渡效果。以下是一個簡單的示例:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Hover Animation</title>
</head>
<body>
<div class="box"></div>
</body>
</html>
CSS (styles.css):
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: all 0.5s ease; /* 添加過渡效果 */
}
.box:hover {
background-color: red; /* 鼠標懸停時的背景顏色 */
transform: scale(1.2); /* 鼠標懸停時放大1.2倍 */
}
在這個示例中,當鼠標懸停在.box
元素上時,背景顏色會在0.5秒內過渡到紅色,同時元素會放大1.2倍。transition
屬性中的all
表示所有屬性都會有過渡效果,0.5s
表示過渡時間,ease
表示過渡效果的速度曲線。你可以根據需要調整這些值。