Greasy Fork

点击屏幕生成爱心特效

点击屏幕时显示爱心特效

目前为 2024-12-05 提交的版本。查看 最新版本

// ==UserScript==
// @name         点击屏幕生成爱心特效
// @namespace    http://tampermonkey.net/
// @version      0.9
// @description  点击屏幕时显示爱心特效
// @author       You
// @match        *://*/*
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 添加爱心动画的样式
   GM_addStyle(`
        .heart {  
            position: absolute;  
            z-index: 10; /* 确保爱心在最上层 */  
            width: 150px;  
            height: 135px;  
            background: pink;  
            transform: rotate(45deg);  
            animation: heartbeat 1s infinite;  
        }  
        .heart::before,  
        .heart::after {  
            content: "";  
            position: absolute;  
            width: 150px;  
            height: 135px;  
            background: pink;  
            border-radius: 50%;  
        }  
        .heart::before {  
            left: -75px;  
            border-radius: 50% 0 0 50%;  
        }  
        .heart::after {  
            top: -75px;  
            border-radius: 50% 50% 0 0;  
        }  

        @keyframes heartAnimation {
            0% {
                transform: scale(0) rotate(45deg);
                opacity: 1;
            }
            100% {
                transform: scale(2) rotate(45deg);
                opacity: 0;
            }
        }
    `);

    // 确保页面完全加载
    window.addEventListener('load', function() {
        console.log('页面已加载');

        // 监听鼠标点击事件
        document.addEventListener('click', function(event) {
            // 获取页面滚动的距离
            const scrollX = window.scrollX || window.pageXOffset;
            const scrollY = window.scrollY || window.pageYOffset;

            // 获取页面相对于视口的坐标
            const rect = document.body.getBoundingClientRect();

            // 计算点击位置相对于页面的坐标
            const x = event.clientX + scrollX - rect.left;
            const y = event.clientY + scrollY - rect.top;

            // 创建爱心元素
            const heart = document.createElement('div');
            heart.classList.add('heart');

            // 设置爱心的位置为点击位置
            heart.style.left = `${x - 25}px`;  // 调整位置使爱心的中心对准点击点
            heart.style.top = `${y - 25}px`;

            // 将爱心元素添加到页面中
            document.body.appendChild(heart);

            // 设置定时器,1秒后删除爱心元素
            setTimeout(() => {
                heart.remove();
            }, 1000);
        });
    });

})();