css+js 实现鼠标尾巴气泡跟随特效

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鼠标尾巴跟随泡泡特效</title>
    <style>
        body {
            overflow: hidden;
        }

        .circle {
            position: absolute;
            pointer-events: none;
            /* 确保这个元素不会影响鼠标事件 */
            opacity: 1;
            transition: opacity 0.8s ease-out;
            /* 渐隐效果 */
            box-shadow: 0px 0px 8px 10px #fff inset;
            border-radius: 50%;
        }
    </style>
</head>

<body>

    <script>
        // 这里将添加JavaScript代码
        function createcircle(event) {
            const circle = document.createElement('div');
            circle.classList.add('circle');
            document.body.appendChild(circle);

            // 设置泡泡位置
            circle.style.left = `${event.clientX}px`;
            circle.style.top = `${event.clientY}px`;

            // 设置泡泡大小和其他样式属性
            const size = Math.floor(Math.random() * 10 + 20); // 随机生成5到15之间的大小
            circle.style.width = `${size}px`;
            circle.style.height = `${size}px`;
            // circle.style.boxShadow = `hsl(${Math.random() * 360}, 100%, 50%)`; // 随机颜色
            circle.style.boxShadow = ` 0px 0px 8px 0px hsl(${Math.random() * 360}, 90%, 70%) inset`

            // 设置泡泡的运动轨迹
            const destinationX = event.clientX + (Math.random() - 0.5) * 2 * 75;
            const destinationY = event.clientY + (Math.random() - 0.5) * 2 * 75;
            const animation = circle.animate([
                {
                    transform: `translate(0, 0)`,
                    opacity: 1
                },
                {
                    transform: `translate(${destinationX - event.clientX}px, ${destinationY - event.clientY}px)`,
                    opacity: 1
                }
            ], {
                duration: Math.random() * 1000 + 500, // 随机动画持续时间
                easing: 'ease-out'
            });

            // 动画完成后移除泡泡
            animation.onfinish = () => {
                circle.remove();
            };
        }
        document.addEventListener('mousemove', createcircle);

    </script>

</body>

</html>

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值