vue3单页面添加鼠标拖尾效果

MouseTrailing.js文件
let particles = [];
let possibleColors = ["#D61C59", "#E7D84B", "#1B8798"]

let width = window.innerWidth;
let height = window.innerHeight;
let cursor = { x: width / 2, y: width / 2 };
let isActive = false; // 用于控制动画是否继续运行

function onMouseMove(e) {
    cursor.x = e.clientX;
    cursor.y = e.clientY;

    addParticle(cursor.x, cursor.y, possibleColors[Math.floor(Math.random() * possibleColors.length)]);
}

function onWindowResize(e) {
    width = window.innerWidth;
    height = window.innerHeight;
}

function addParticle(x, y, color) {
    var particle = new Particle();
    particle.init(x, y, color);
    particles.push(particle);
}

function updateParticles() {
    for (var i = 0; i < particles.length; i++) {
        particles[i].update();
    }

    for (var i = particles.length - 1; i >= 0; i--) {
        if (particles[i].lifeSpan < 0) {
            particles[i].die();
            particles.splice(i, 1);
        }
    }
}

function Particle() {
    this.character = "*";
    this.lifeSpan = 120; // ms
    this.initialStyles = {
        "position": "fixed",
        "display": "inline-block",
        "top": "0px",
        "left": "0px",
        "pointerEvents": "none",
        "touch-action": "none",
        "z-index": "10000000",
        "fontSize": "25px",
        "will-change": "transform"
    };

    this.init = function (x, y, color) {
        this.velocity = {
            x: (Math.random() < 0.5 ? -1 : 1) * (Math.random() / 2),
            y: 1
        };

        this.position = { x: x + 10, y: y + 10 };
        this.initialStyles.color = color;

        this.element = document.createElement('span');
        this.element.innerHTML = this.character;
        applyProperties(this.element, this.initialStyles);
        this.update();

        const container = document.querySelector('.js-cursor-container');
        if (container) {
            container.appendChild(this.element);
        } else {
            document.body.appendChild(this.element);
        }
    };

    this.update = function () {
        this.position.x += this.velocity.x;
        this.position.y += this.velocity.y;
        this.lifeSpan--;

        this.element.style.transform = "translate3d(" + this.position.x + "px," + this.position.y + "px, 0) scale(" + (this.lifeSpan / 120) + ")";
    }

    this.die = function () {
        if (this.element.parentNode) {
            this.element.parentNode.removeChild(this.element);
        }
    }
}

function applyProperties(target, properties) {
    for (var key in properties) {
        target.style[key] = properties[key];
    }
}

export function fairyDustCursor() {
    particles = [];
    isActive = true;

    function init() {
        bindEvents();
        loop();
    }

    function bindEvents() {
        document.addEventListener('mousemove', onMouseMove);
        window.addEventListener('resize', onWindowResize);
    }

    function loop() {
        if (!isActive) return;
        requestAnimationFrame(loop);
        updateParticles();
    }

    if (!('ontouchstart' in window || navigator.msMaxTouchPoints)) init();
}

export function cleanupFairyDustCursor() {
    isActive = false;
    document.removeEventListener('mousemove', onMouseMove);
    window.removeEventListener('resize', onWindowResize);

    particles.forEach(particle => {
        particle.die();
    });
    particles = [];
}
在想要使用的页面引入js文件,添加挂载以及销毁函数,一定要在想要的范围内添加 class="js-cursor-container"
<template>
  <router-link tag="button" to="/goToPage" class="router-link">to mumu navigation</router-link>
</template>

<script>
import { fairyDustCursor, cleanupFairyDustCursor } from '../../../assets/js/style/MouseTrailing.js';
import { onMounted, onBeforeUnmount } from 'vue';

export default {
  setup() {
    onMounted(() => {
      fairyDustCursor();
    });

    onBeforeUnmount(() => {
      cleanupFairyDustCursor();
    });
  }
}
</script>

 

 
点赞

当前页面评论已关闭。

隐藏
变装