页面点击出现小心心。

// 页面点击出现小心心
!function(){
    // 创建一个style标签添加到head下面,并写好样式
    let windowHeartStyle = document.createElement("style");
    let styleType = document.createTextNode(`
        .windowHeart{
            width:10px;
            height:10px;
            background-color:red;
            position:fixed;
            top:500px;
            left:500px;
            transform:rotate(45deg);
            z-index:999;
            opacity:.8;
        }
        .windowHeart:after,.windowHeart:before{
            content: '';
            width: inherit;
            height: inherit;
            background: inherit;
            border-radius: 50%;
            position: absolute;
        }
        .windowHeart:after{
            top: -5px;
        }
        .windowHeart:before{
            left: -5px;
        }
    `);
    windowHeartStyle.appendChild(styleType)
    document.head.appendChild(windowHeartStyle)
    
    // 窗口的点击事件
    window.onclick = function(e){
        // 获取点击坐标
        let clientX = e.clientX;
        let clientY = e.clientY;

        // 创建一个div标签
        let windowHeart = document.createElement("div");

        // 标签添加class名
        windowHeart.className = "windowHeart";

        // 鼠标点击的地方出现小心心
        windowHeart.style.top = clientY - 12 + "px";
        windowHeart.style.left = clientX - 5 + "px";

        // 随机颜色
        function rand (){
            return Math.floor(Math.random()*255);
        }
        windowHeart.style.backgroundColor = `rgb(${rand ()},${rand ()},${rand ()})`;

        // body添加元素
        document.body.appendChild(windowHeart)

        // 向上方移动逐渐消失
        let count = 0;
        let AnimationFrame;
        windowHeart.setAttribute("count",count)
        function a (){
            count++;
            windowHeart.setAttribute("count",count);
            windowHeart.style.transform= `translateY(${count * -1+ "px"}) rotate(45deg)`;
            windowHeart.style.opacity= `${1-count/100}`;
            AnimationFrame = requestAnimationFrame(a);
            if(count >= 100){
                document.body.removeChild(windowHeart)
                cancelAnimationFrame(AnimationFrame);
            }
        }
        requestAnimationFrame(a)
    }
}()

requestAnimationFrame()来一个大佬帮忙解释清楚这个东西呗。不是特别明白。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值