来吧,魔法棒走起!

826 阅读2分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第11天,点击查看活动详情

我正在参加 码上掘金体验活动,详情:show出你的创意代码块

需求

本来参加日更打卡,正愁没有思路,正好遇到了“码上掘金体验活动”,更让人欣慰的是两个活动居然可以同时参加,那必须搞起来。结合“码上掘金体验活动”的创作要求,自己决定接下来几天,搞一个利用HTML和CSS编写各种酷炫页面的文章集合。今天是第三天,昨天的文章请看《来吧,蜘蛛网走起!》

今天我们来实现一个魔法棒效果,移动鼠标时,会带着一条长长的尾巴!

实现

HTML 代码

html代码块:

<!DOCTYPE html>
<html lang="en">
 
<head>
</head>
 
<body>
    <span class="js-cursor-container"></span>
    <script>  
    </script>
</body>
 
</html>

这段代码主要是为了满足“码上掘金体验活动”对 HTML 页面的要求。

JavaScript 代码

接下来看 JavaScript 代码块,为了避免解说过于分散,相关的介绍内容以注释的形式进行展开。

 (function fairyDustCursor() {
 
            var possibleColors = ["#D61C59", "#E7D84B", "#1B8798"]
            var width = window.innerWidth;
            var height = window.innerHeight;
            var cursor = { x: width / 2, y: width / 2 };
            var particles = [];
 
            function init() {
                bindEvents();
                loop();
            }
 
            // 绑定相关的事件方法,包括鼠标移动和窗口变化
            function bindEvents() {
                document.addEventListener('mousemove', onMouseMove);
                window.addEventListener('resize', onWindowResize);
            }
            // 显示窗口自适应
            function onWindowResize(e) {
                width = window.innerWidth;
                height = window.innerHeight;
            }
 
            // 移动鼠标时,改变星星的状态
            function onMouseMove(e) {
                cursor.x = e.clientX;
                cursor.y = e.clientY;
 
                addParticle(cursor.x, cursor.y, possibleColors[Math.floor(Math.random() * possibleColors.length)]);
            }
            // 增加新的星星
            function addParticle(x, y, color) {
                var particle = new Particle();
                particle.init(x, y, color);
                particles.push(particle);
            }
            // 更新星星状态
            function updateParticles() {
 
                // Updated
                for (var i = 0; i < particles.length; i++) {
                    particles[i].update();
                }
 
                // Remove dead particles
                for (var i = particles.length - 1; i >= 0; i--) {
                    if (particles[i].lifeSpan < 0) {
                        particles[i].die();
                        particles.splice(i, 1);
                    }
                }
 
            }
            // 循环效果
            function loop() {
                requestAnimationFrame(loop);
                updateParticles();
            }
 
            // 魔法棒的星星
            function Particle() {
 
                this.character = "*";
                this.lifeSpan = 200; //ms
                this.initialStyles = {
                    "position": "fixed",
                    "display": "inline-block",
                    "top": "0px",
                    "left": "0px",
                    "pointerEvents": "none",
                    "touch-action": "none",
                    "z-index": "10000000",
                    "fontSize": "35px",
                    "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();
 
                    document.querySelector('.js-cursor-container').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 () {
                    this.element.parentNode.removeChild(this.element);
                }
 
            }
 
            // 为元素设置 css `properties` 属性.
            function applyProperties(target, properties) {
                for (var key in properties) {
                    target.style[key] = properties[key];
                }
            }
 
            if (!('ontouchstart' in window || navigator.msMaxTouchPoints)) init();
        })();  

效果展示

好了,介绍了那么多,我们来看看最终的效果吧,是不是迫不及待了,请看下图:

image.png

项目地址如下:

code.juejin.cn/pen/7087954…


作者简介:😄大家好,我是 Data-Mining(liuzhen007),是一位典型的音视频技术爱好者,前后就职于传统广电巨头和音视频互联网公司,具有丰富的音视频直播和点播相关经验,对 WebRTC、FFmpeg 和 Electron 有非常深入的了解,😄公众号:玩转音视频。同时也是 CSDN 博客专家、华为云享专家(共创编辑)、InfoQ 签约作者,欢迎关注我分享更多干货!😄