溫馨提示×

CSS中hover能實現旋轉效果嗎

css
小樊
115
2024-10-21 19:19:18
欄目: 編程語言

是的,在CSS中,可以使用@keyframes規則和:hover偽類選擇器結合來實現元素的旋轉效果。下面是一個簡單的示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        .box {
            width: 100px;
            height: 100px;
            background-color: red;
            transition: transform 0.5s;
        }

        .box:hover {
            animation: rotation 2s linear infinite;
        }

        @keyframes rotation {
            from {
                transform: rotate(0deg);
            }
            to {
                transform: rotate(359deg);
            }
        }
    </style>
</head>
<body>
    <div class="box"></div>
</body>
</html>

在這個示例中,當鼠標懸停在.box元素上時,它會開始旋轉。@keyframes定義了一個名為rotation的動畫,從0度開始,到359度結束,實現了360度的旋轉效果。transition屬性使得旋轉過程更加平滑。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女