鼠标移动图片光标动画特效

鼠标移动图片光标动画特效实现

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鼠标移动图片光标动画特效</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f0f0f0;
            overflow: hidden;
            font-family: Arial, sans-serif;
            cursor: none;
        }
        
        .content {
            text-align: center;
            padding: 20px;
        }
        
        h1 {
            margin-bottom: 20px;
            color: #333;
        }
        
        .custom-cursor {
            position: fixed;
            width: 60px;
            height: 60px;
            background-image: url('https://via.placeholder.com/60');
            background-size: cover;
            border-radius: 50%;
            pointer-events: none;
            transform: translate(-50%, -50%);
            mix-blend-mode: multiply;
            z-index: 9999;
            transition: transform 0.1s ease;
        }
        
        .link {
            margin-top: 30px;
            color: #666;
            font-size: 14px;
        }
        
        .link a {
            color: #0066cc;
            text-decoration: none;
        }
        
        .link a:hover {
            text-decoration: underline;
        }
        
        .image-container {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 20px;
            margin-top: 30px;
        }
        
        .image-item {
            width: 200px;
            height: 150px;
            overflow: hidden;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease;
        }
        
        .image-item:hover {
            transform: scale(1.05);
        }
        
        .image-item img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.5s ease;
        }
        
        .image-item:hover img {
            transform: scale(1.1);
        }
    </style>
</head>
<body>
    <div class="custom-cursor"></div>
    
    <div class="content">
        <h1>鼠标移动图片光标动画特效</h1>
        
        <div class="image-container">
            <div class="image-item">
                <img src="https://via.placeholder.com/200x150/ff9999" alt="图片1">
            </div>
            <div class="image-item">
                <img src="https://via.placeholder.com/200x150/99ff99" alt="图片2">
            </div>
            <div class="image-item">
                <img src="https://via.placeholder.com/200x150/9999ff" alt="图片3">
            </div>
        </div>
        

    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const cursor = document.querySelector('.custom-cursor');
            const links = document.querySelectorAll('a, .image-item');
            
            // 鼠标移动时更新光标位置
            document.addEventListener('mousemove', function(e) {
                cursor.style.left = e.clientX + 'px';
                cursor.style.top = e.clientY + 'px';
                
                // 添加弹性效果
                setTimeout(() => {
                    cursor.style.transform = `translate(-50%, -50%) scale(1)`;
                }, 100);
            });
            
            // 鼠标按下时改变光标样式
            document.addEventListener('mousedown', function() {
                cursor.style.transform = `translate(-50%, -50%) scale(0.8)`;
            });
            
            // 鼠标释放时恢复光标样式
            document.addEventListener('mouseup', function() {
                cursor.style.transform = `translate(-50%, -50%) scale(1)`;
            });
            
            // 鼠标悬停在链接或图片上时改变光标样式
            links.forEach(link => {
                link.addEventListener('mouseover', function() {
                    cursor.style.transform = `translate(-50%, -50%) scale(1.5)`;
                    cursor.style.opacity = '0.8';
                });
                
                link.addEventListener('mouseout', function() {
                    cursor.style.transform = `translate(-50%, -50%) scale(1)`;
                    cursor.style.opacity = '1';
                });
            });
            
            // 随机改变光标图片
            function changeCursorImage() {
                const images = [
                    'https://via.placeholder.com/60/ff9999',
                    'https://via.placeholder.com/60/99ff99',
                    'https://via.placeholder.com/60/9999ff',
                    'https://via.placeholder.com/60/ffff99'
                ];
                const randomImage = images[Math.floor(Math.random() * images.length)];
                cursor.style.backgroundImage = `url('${randomImage}')`;
                
                setTimeout(changeCursorImage, 2000);
            }
            
            // 启动光标图片变化
            changeCursorImage();
        });
    </script>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值