自定义旋转

86 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        @keyframes spin {
            from {
                transform: rotate(0deg);
            }
            to {
                transform: rotate(360deg);
            }
        }
        /* .bg-spin {
            animation: spin 2s linear infinite;
        } */
        .content {
            position: relative;
        }
        .content::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-image: url('https://tse1-mm.cn.bing.net/th/id/OIP-C.l_gTd3AZMeJMoDtHgnoFvAHaHn?rs=1&pid=ImgDetMain');
            background-size: cover;
            background-position: center;
            animation: spin 2s linear infinite;
            z-index: 1;
        }
        .paused::before {
            animation-play-state: paused;
        }
        .text {
            position: relative;
            z-index: 2;
        }
    </style>
</head>
<body class="m-0 h-screen flex flex-col justify-center items-center gap-4">
    <div class="w-[400px] h-[400px] bg-blue-200 flex justify-center items-center">
        <div id="spinContainer" class="w-[200px] h-[200px] flex justify-center items-center content paused">
            <span class="text-[20px] text-pink-500 text">loading</span>
        </div>
    </div>
    
    <button onclick="toggleSpin()" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">
        开始/暂停旋转
    </button>

    <script>
        function toggleSpin() {
            const container = document.getElementById('spinContainer');
            container.classList.toggle('paused');
        }
    </script>
</body>
</html>