js实现鼠标点击出现爱心

<!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>Document</title>
  <style>
       @keyframes remove {
            100% {
                opacity: 0;
            }
        }
  </style>
</head>
<body>
   
    <script>
          function createLove(w,h,color,x,y){
      const span=document.createElement('div');
      const left=document.createElement('div');
      const right=document.createElement('div');
      const spanStyle={
        width:w+'px',
        height:h+'px',
        position:'relative',
        top:y+'px',
        left:x+'px',
        backgroundColor:color,
        transform: 'rotate(-45deg)'
      }
      const leftStyle={
       width:w+'px',
       height:h+'px',
       position:'absolute',
       top:-w/2+'px',
       left:0+'px',
       borderRadius:'50%',
       backgroundColor:color,
      }
      const rightStyle={
        width:w+'px',
        height:h+'px',
        position:'absolute',
        borderRadius:'50%',
        top:0+'px',
        left:w/2+'px',
        backgroundColor:color,
      }
    //   span.style=spanStyle;
    //   left.style=left;
    //   right.style=right;
    Object.assign(span.style,spanStyle);
    Object.assign(left.style,leftStyle);
    Object.assign(right.style,rightStyle);
    //   document.body.appendChild(span);
      span.appendChild(left);
      span.appendChild(right);
    //   console.log(span)
    return span;
      
    }

        let span= createLove(100,100,'red',900,100);
        console.log(span)
        function getRandom(){
          let r = Math.floor(Math.random() * 256);
          let g = Math.floor(Math.random() * 256);
          let b = Math.floor(Math.random() * 256);
          let color = "#" + r.toString(16) + g.toString(16) + b.toString(16);
          return color;
        }
        console.log(getRandom())
       
        window.addEventListener('load',function(){
            let body=document.body;
            document.addEventListener('click',function(e){
            // console.log('sds')
            let x = e.pageX;
            let y = e.pageY;
            // console.log(x)
            let color= getRandom();
           let span= createLove(15,15,color,x,y);
            span.style.animation='remove 1s'
            body.appendChild(span) 
         
            setTimeout(function(){
                span.remove()
            },900)

        })
        })
        
    </script>
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值