关于JS控制CSS3动画(@keyframes)的一个小技巧


<!-- 购物车效果-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin:0;
      padding:0;
    }
    .goods{
      width:100%;
      height:100vh;
    }
    .goods>.goodsList{
      height:15vh;
      border-top:1px solid #ddd;
    }
    .goods>.goodsList>.goodsListButton{
      float: right;
      margin-top:2vh;
      margin-right:3vh;
    }
    .goods>.goodsList:last-child{
      border-bottom:1px solid #ddd;
    }
    .goods>.goodsList>div{  /*生成的div*/
      width:30px;
      height:30px;
      background:red;
      animation:mymove 1s;
      position:absolute;
    }
    /* @keyframes mymove {
        0% {top:0px;}
        10% {left:65.5%}
        100% {top:100%;left:65.5%;}
    } */
    footer{
      position:fixed;
      width:100%;
      display: flex;
      bottom:0;
      background:red;
      border-top:1px solid red;
      height:8vh;
    }
    footer>div{
      width:25vw;
      height:100%;
      line-height:8vh;
      font-size:13px;
      color:#fff;
      text-align: center;
    }
    footer>div:nth-child(3){
      background:#ddd;
    }
  </style>
</head>
<body>
    <div class="goods">
      <div class="goodsList"><button class="goodsListButton">加入购物车</button></div>
      <div class="goodsList"><button class="goodsListButton">加入购物车</button></div>
      <div class="goodsList"><button class="goodsListButton">加入购物车</button></div>
      <div class="goodsList"><button class="goodsListButton">加入购物车</button></div>
    </div>
    <footer>
       <div>1</div>
       <div>2</div>
       <div>购物车</div>
       <div>4</div>
    </footer>
</body>
    <script>

        //添加css规则
        function addCSSRule(sheet, selector, rules, index) {
            if("insertRule" in sheet) {
                sheet.insertRule(selector + "{" + rules + "}", index);
            }else if("addRule" in sheet) {
                sheet.addRule(selector, rules, index);
            }
        }
        //删除CSS规则
        function delCSSRule(sheet){
          sheet.deleteRule(0)
        }
        
        
        //预加载
        window.onload = function(){
          const goods = document.querySelector(".goods");
          goods.onclick = function(e){
              const target = e.target;
              if(target.tagName == "BUTTON"){
                const goodsListButton = document.querySelectorAll(".goodsListButton");
                const clientX = e.clientX; //当前X坐标
                const clientY = e.clientY; //当前Y坐标
                var div = document.createElement("div");target.after(div); //生成DIV节点
                var divNode = target.parentNode.getElementsByTagName("div")[0]; 
                divNode.style.top = (clientY - divNode.clientWidth / 2) + "px"; //X坐标赋值
                divNode.style.left = (clientX - divNode.clientHeight / 2) + "px"; //Y坐标赋值

                for(var i=0; i < goodsListButton.length; i++){
                  goodsListButton[i].disabled = true;  //禁止多次点击
                }
                  
                //动态添加规则  
                addCSSRule(document.styleSheets[0], "@keyframes mymove", `0% {top:${clientY}px;}20% {left:65.5%}100% {top:100%;left:65.5%;}`);
                //删除规则
                setTimeout(function(){
                     delCSSRule(document.styleSheets[0]);
                     divNode.parentNode.removeChild(divNode);
                     for(var i=0; i < goodsListButton.length; i++){
                        goodsListButton[i].disabled = false; 
                     }
                },1000)
              }
          }
        }
    </script>
</html>
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值