鼠标粒子拖尾特效

<!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 {
            background: #121212;
            min-height: 100vh;
            overflow: hidden;
            font-family: 'Arial', sans-serif;
            cursor: none;
        }
        
        .container {
            position: relative;
            width: 100%;
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            z-index: 10;
            color: white;
            text-align: center;
            padding: 20px;
        }
        
        h1 {
            font-size: 3rem;
            margin-bottom: 20px;
            background: linear-gradient(90deg, #ff8a00, #e52e71, #00b4db);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            text-shadow: 0 0 10px rgba(255,255,255,0.2);
        }
        
        p {
            font-size: 1.2rem;
            margin-bottom: 30px;
            max-width: 600px;
            line-height: 1.6;
            color: rgba(255,255,255,0.8);
        }
        
        .link-btn {
            display: inline-block;
            padding: 12px 30px;
            background: linear-gradient(45deg, #00b4db, #0083b0);
            color: white;
            text-decoration: none;
            border-radius: 50px;
            font-weight: bold;
            font-size: 1.1rem;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(0,180,219,0.4);
            margin-top: 20px;
        }
        
        .link-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(0,180,219,0.6);
        }
        
        .footer {
            position: fixed;
            bottom: 20px;
            width: 100%;
            text-align: center;
            color: rgba(255,255,255,0.5);
            font-size: 0.9rem;
            z-index: 10;
        }
        
        .footer a {
            color: rgba(255,255,255,0.8);
            text-decoration: none;
        }
        
        .footer a:hover {
            color: white;
            text-decoration: underline;
        }
        
        /* 粒子样式 */
        .particle {
            position: absolute;
            border-radius: 50%;
            pointer-events: none;
            transform: translate(-50%, -50%);
            mix-blend-mode: screen;
            transition: transform 0.1s ease-out;
        }
        
        /* 背景装饰粒子 */
        .bg-particle {
            position: absolute;
            border-radius: 50%;
            background: rgba(255,255,255,0.05);
            animation: float 15s infinite linear;
        }
        
        @keyframes float {
            0% {
                transform: translateY(0) rotate(0deg);
            }
            100% {
                transform: translateY(-100vh) rotate(360deg);
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>鼠标粒子拖尾特效</h1>
        <p>移动鼠标查看彩色粒子跟随效果。每个粒子都有独特的颜色和大小,形成流畅的拖尾动画。</p>

    </div>
    
    <div class="footer">

    </div>

    <script>
        // 粒子系统
        class ParticleSystem {
            constructor() {
                this.particles = [];
                this.particleCount = 30; // 拖尾粒子数量
                this.mouseX = window.innerWidth / 2;
                this.mouseY = window.innerHeight / 2;
                this.init();
            }
            
            init() {
                // 创建背景粒子
                this.createBackgroundParticles();
                
                // 创建拖尾粒子
                for (let i = 0; i < this.particleCount; i++) {
                    this.createParticle(i);
                }
                
                // 监听鼠标移动
                document.addEventListener('mousemove', (e) => {
                    this.mouseX = e.clientX;
                    this.mouseY = e.clientY;
                });
                
                // 开始动画
                this.animate();
            }
            
            createBackgroundParticles() {
                const particleCount = 30;
                for (let i = 0; i < particleCount; i++) {
                    const particle = document.createElement('div');
                    particle.classList.add('bg-particle');
                    
                    // 随机大小
                    const size = Math.random() * 10 + 5;
                    particle.style.width = `${size}px`;
                    particle.style.height = `${size}px`;
                    
                    // 随机位置
                    particle.style.left = `${Math.random() * 100}%`;
                    particle.style.top = `${Math.random() * 100 + 100}%`;
                    
                    // 随机动画
                    particle.style.animationDuration = `${Math.random() * 10 + 10}s`;
                    particle.style.animationDelay = `${Math.random() * 5}s`;
                    
                    document.body.appendChild(particle);
                }
            }
            
            createParticle(index) {
                const particle = document.createElement('div');
                particle.classList.add('particle');
                document.body.appendChild(particle);
                
                this.particles.push({
                    element: particle,
                    x: this.mouseX,
                    y: this.mouseY,
                    size: Math.random() * 15 + 5,
                    color: this.getRandomColor(),
                    delay: index * 0.03,
                    speed: Math.random() * 0.2 + 0.05
                });
            }
            
            getRandomColor() {
                const hue = Math.floor(Math.random() * 360);
                return `hsl(${hue}, 100%, 70%)`;
            }
            
            animate() {
                this.particles.forEach((particle, index) => {
                    // 计算延迟因子
                    const delayFactor = 1 - (particle.delay * 2);
                    
                    // 计算目标位置
                    const targetX = this.mouseX;
                    const targetY = this.mouseY;
                    
                    // 更新粒子位置(带有延迟效果)
                    particle.x += (targetX - particle.x) * particle.speed * delayFactor;
                    particle.y += (targetY - particle.y) * particle.speed * delayFactor;
                    
                    // 更新粒子样式
                    particle.element.style.left = `${particle.x}px`;
                    particle.element.style.top = `${particle.y}px`;
                    particle.element.style.width = `${particle.size * delayFactor}px`;
                    particle.element.style.height = `${particle.size * delayFactor}px`;
                    particle.element.style.backgroundColor = particle.color;
                    particle.element.style.opacity = delayFactor * 0.8;
                    particle.element.style.filter = `blur(${2 * (1 - delayFactor)}px)`;
                });
                
                requestAnimationFrame(() => this.animate());
            }
        }
        
        // 初始化粒子系统
        window.addEventListener('load', () => {
            new ParticleSystem();
        });
    </script>
</body>
</html>

### 实现鼠标特效 为了在 Vue.js 中实现鼠标特效,可以借鉴基于 Canvas 的方法来创建动态视觉效果。Canvas 提供了一个强大的绘图环境,在其中可以通过 JavaScript 控制绘制路径、颜色以及其他图形属性。 下面是一个简单的例子,展示了如何利用 HTML5 `<canvas>` 和 JavaScript 来制作一个跟随鼠标的彗星效果[^3]: ```html <template> <div id="app"> <!-- 定义 canvas --> <canvas ref="trailCanvas"></canvas> </div> </template> <script> export default { mounted() { const canvas = this.$refs.trailCanvas; let ctx, w, h; function initCanvas() { // 设置画布大小等于窗口尺寸 w = window.innerWidth; h = window.innerHeight; canvas.width = w; canvas.height = h; // 获取上下文并设置样式 ctx = canvas.getContext(&#39;2d&#39;); ctx.strokeStyle = &#39;#fff&#39;; ctx.lineWidth = 1.5; ctx.lineCap = &#39;round&#39;; // 初始化粒子数组 particlesArray = []; } class Particle { constructor(x, y) { this.x = x; this.y = y; this.size = Math.random() * 1 + 0.5; this.speedX = (Math.random() - 0.5) * 2; this.speedY = (Math.random() - 0.5) * 2; this.alpha = 1; } update() { this.x += this.speedX; this.y += this.speedY; if(this.size > 0.2)this.size -= 0.05; if(this.alpha > 0.05)this.alpha -= 0.01; } draw() { if(this.alpha <= 0){ return; }else{ ctx.globalAlpha = this.alpha; ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI*2); ctx.closePath(); ctx.fill(); } } } let mouse = {x: null, y: null}; document.addEventListener(&#39;mousemove&#39;, event => { mouse.x = event.x; mouse.y = event.y; createParticles(mouse.x, mouse.y); }); function createParticles(x, y) { for(let i = 0; i < 5; i++){ particlesArray.push(new Particle(x,y)); } } function animate(){ requestAnimationFrame(animate); ctx.clearRect(0, 0, w, h); for(const particle of particlesArray){ particle.update(); particle.draw(); } } initCanvas(); animate(); window.onresize = () => { initCanvas(); }; }, } </script> <style scoped> #app { position: relative; } canvas { display:block; background-color:#000; } </style> ``` 此代码片段定义了一种方式来响应用户的鼠标移动事件,并通过 `Particle` 类模拟多个微粒的行为,这些微粒会逐渐消失形成效应。每当检测到新的鼠标位置时就会生成一批新粒子,它们会在屏幕上短暂存在一段时间后慢慢淡出视野。 #### 注意事项 - 上述示例适用于现代浏览器支持的场景;对于旧版IE可能需要额外处理。 - 如果页面中有其他交互元素,则需考虑调整 z-index 或者采取措施防止遮挡重要操作区域。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值