Native JS realizes the special effect of spreading love by mouse sliding

Native JS realizes the special effect of spreading love by mouse sliding

This article shares with you a js special effect of spreading love when the mouse slides, the effect is as follows:

The following is the code implementation, you are welcome to copy, paste and collect it.

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Native JS to achieve mouse sliding love effect</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            font-family: 'Microsoft YaHei', sans-serif;
        }
 
        body {
            height: 100vh;
            background: #000;
            overflow: hidden;
        }
 
        span {
            position: absolute;
            background: url(heart.png);
            pointer-events: none;
            width: 100px;
            height: 100px;
            background-size: cover;
            transform: translate(-50%, -50%);
            animation: animate 2s linear infinite;
        }
 
        @keyframes animate {
            0% {
                transform: translate(-50%, -50%);
                opacity: 1;
                filter: hue-rotate(0deg);
            }
 
            100% {
                transform: translate(-50%, -1000%);
                opacity: 0;
                filter: hue-rotate(360deg);
            }
        }
    </style>
</head>
 
<body>
    <script>
        document.addEventListener('mousemove', (e) => {
            let body = document.querySelector('body')
            let heart = document.createElement('span')
            let x = e.offsetX
            let y = e.offsetY
            heart.style.left = x + 'px'
            heart.style.top = y + 'px'
            let size = Math.random() * 100
            heart.style.width = size + 'px'
            heart.style.height = size + 'px'
            body.appendChild(heart)
            setTimeout(() => {
                heart.remove()
            }, 3000)
        })
    </script>
</body>
 
</html>

The following is the picture heart.png introduced in the above code

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:
  • js realizes the floating heart effect when clicking the mouse

<<:  Docker uses Supervisor to manage process operations

>>:  How to safely shut down MySQL

Recommend

How to strike a balance between ease of use and security in the login interface

Whether you are a web designer or a UI designer, ...

The process of SSH service based on key authentication in Linux system

As we all know, SSH is currently the most reliabl...

Detailed explanation of BOM and DOM in JavaScript

Table of contents BOM (Browser Object Model) 1. W...

Solution to mysql prompt "got timeout reading communication packets"

Error message: user: 'root' host: `localh...

CnBlogs custom blog style sharing

After spending half the night on it, I finally ma...

Docker builds cluster MongoDB implementation steps

Preface Due to the needs of the company's bus...

How to make vue long list load quickly

Table of contents background Main content 1. Comp...

JavaScript imitates Taobao magnifying glass effect

This article shares the specific code for JavaScr...

How to draw a vertical line between two div tags in HTML

Recently, when I was drawing an interface, I enco...

Node.js+postman to simulate HTTP server and client interaction

Table of contents 1. Node builds HTTP server 2. H...

JavaScript uses canvas to draw coordinates and lines

This article shares the specific code of using ca...