JavaScript to achieve mouse tailing effect

JavaScript to achieve mouse tailing effect

Mouse effects require the use of setTimeout to generate nodes at fixed times, delete nodes, and assign random widths, heights, and colors to the generated nodes, so that each effect node looks different.

Note: The generated node needs to be absolutely positioned so that it is out of the document flow and does not affect the position of other elements on the page.

Code example:

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mouse Effects</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            background-color: #9df;
            overflow: hidden;
            height: 100vh;
        }
        
        span {
            height: 30px;
            width: 30px;
            border-radius: 50%;
            position: absolute;
            pointer-events: none;
            transform: translate(-50%, -50%);
            box-shadow: 10px 10px 30px #45f, -10px -10px 30px #d80;
            animation: box 5s linear infinite;
            z-index: 3;
        }
        
        @keyframes box {
            0% {
                transform: translate(-50%, -50%);
                opacity: 1;
                filter: hue-rotate(0deg);
            }
            100% {
                transform: translate(-50%, -1000%);
                opacity: 1;
                filter: hue-rotate(720deg);
            }
        }
    </style>
</head>
 
<body>
 
</body>
 
</html>
<script>
    document.addEventListener("mousemove", function(e) {
        var body = document.querySelector("body");
        var span = document.createElement("span");
        var x = e.offsetX
        var y = e.offsetY
        span.style.left = x + "px"
        span.style.top = y + "px";
 
        console.log(x + ">>>" + y)
        var a = Math.random() * 30;
        span.style.width = 30 + a + "px";
        span.style.height = 30 + a + "px";
        body.appendChild(span);
        setTimeout(function() {
            span.remove();
            // console.log("ok")
        }, 4500)
    })
</script>

Running results:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Common array operations in JavaScript
  • A simple and in-depth study of async and await in JavaScript
  • JavaScript implementation of classic snake game
  • Javascript basics about built-in objects
  • Detailed explanation of JavaScript operation mechanism and a brief discussion on Event Loop
  • Comparison between Python coroutines and JavaScript coroutines
  • JavaScript to achieve simple drag effect
  • Detailed explanation of JavaScript array deduplication
  • JavaScript to implement the aircraft war game
  • JavaScript setinterval delay one second solution
  • Detailed explanation of JavaScript upload file limit parameter case
  • A brief talk about JavaScript variable promotion
  • In-depth understanding of JavaScript event execution mechanism
  • 8 essential JavaScript code snippets for your project

<<:  Analysis of Linux Zabbix custom monitoring and alarm implementation process

>>:  How to solve the mysql insert garbled problem

Recommend

Let's learn about JavaScript object-oriented

Table of contents JavaScript prototype chain Obje...

Screen command and usage in Linux

Screen Introduction Screen is a free software dev...

Detailed explanation of using echarts map in angular

Table of contents Initialization of echart app-ba...

Docker image import, export, backup and migration operations

Export: docker save -o centos.tar centos:latest #...

26 Commonly Forgotten CSS Tips

This is a collection of commonly used but easily ...

Vue implements tab label (label exceeds automatic scrolling)

When the created tab label exceeds the visible ar...

How to view the IP address of the Docker container

I always thought that Docker had no IP address. I...

Implementation of CSS Fantastic Border Animation Effect

Today I was browsing the blog site - shoptalkshow...

A brief analysis of how to use border and display attributes in CSS

Introduction to border properties border property...

Three ways to implement text color gradient in CSS

In the process of web front-end development, UI d...

Summary of 16 XHTML1.0 and HTML Compatibility Guidelines

1. Avoid declaring the page as XML type . The pag...

CSS pixels and solutions to different mobile screen adaptation issues

Pixel Resolution What we usually call monitor res...

Summary of the most commonly used knowledge points about ES6 new features

Table of contents 1. Keywords 2. Deconstruction 3...