vue实现加入购物车动效

实现

实现逻辑:

  1. 点击添加购物车按钮时,获取当前点击位置eventclientX clientY
  2. 动态创建移动的小球,动态计算小球需要移动到的位置(通过refgetBoundingClientRect获取统计元素按钮位置);
  3. 计算小球最后的移动位置,并在移动到后移除元素
  4. 小球动画移动结束后,添加统计数字。

统计按钮

 <el-button ref="lableBtnRef" @click="handleChangeStep('0')" :class="currentStep == '0' ? 'linear-btn' : ''" class="normal-btn " size="mini">
      <i class="index-circle">1</i>标签选择
      <el-badge :value="cartShopList.length" class="item">
         <i class="lg-icon white-cart"></i>
      </el-badge>
   </el-button>

点击加入购物车按钮:

  //点击加入购物车事件
  async hadnleSelect(event, item) {
      const x = event.clientX - 20;
      const y = event.clientY - 20;
      await this.createBall(x, y)
      //等动画执行完再添加个数
      this.cartShopList.push(item)
    },
    
    //动态创建加入购物车的元素,和动画
    createBall(left, top) {
      return new Promise(reslove => {
        const bar = document.createElement("ball");
        let lableBtn = this.$refs.lableBtnRef.$el
        let rect = lableBtn.getBoundingClientRect() //获取统计按钮位置
        let size = [lableBtn.clientWidth, lableBtn.clientHeight] //统计按钮本身高、宽
        const afterX = rect.x + size[0] / 2 //计算小球最后的位置x
        const afterY = rect.y - (size[1] / 2)//计算小球最后的位置Y
        bar.classList = ['cartBall']
        bar.style.position = "fixed";
        bar.style.left = left + "px";
        bar.style.top = top + "px";
        bar.style.zIndex = '9999'
        bar.style.transition =
          "left .6s linear, top .6s cubic-bezier(0.5, -0.5, 1, 1)";
        document.body.appendChild(bar);
        setTimeout(() => {
          const x = afterX;
          const y = afterY;
          bar.style.top = y + "px";
          bar.style.left = x + "px";
        }, 0);
        bar.ontransitionend = function () {
          this.remove();
          reslove()
        };
      })
    }

//创建的小球的样式
<style>
.cartBall {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  z-index: 99;
  background: url(~@/assets/img/labelManage/sopCart.svg) center /100% no-repeat;
}
</style>

效果图:

在这里插入图片描述

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-05-26 05:58:16       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-26 05:58:16       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-26 05:58:16       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-26 05:58:16       18 阅读

热门阅读

  1. Scala的简单学习二

    2024-05-26 05:58:16       9 阅读
  2. vue中实现动态点击事件名

    2024-05-26 05:58:16       8 阅读
  3. Docker打包nginx镜像丢失挂载的配置文件

    2024-05-26 05:58:16       15 阅读
  4. Leetcode704_二分查找

    2024-05-26 05:58:16       9 阅读
  5. StringMVC

    StringMVC

    2024-05-26 05:58:16      9 阅读
  6. 【MySQL精通之路】SQL优化(1)-查询优化

    2024-05-26 05:58:16       10 阅读
  7. 前端后端是什么

    2024-05-26 05:58:16       12 阅读
  8. SpringBoot配置优先级

    2024-05-26 05:58:16       9 阅读
  9. 第三章 Web Services和 Web Clients简介 - SOAP 标准

    2024-05-26 05:58:16       8 阅读
  10. 类脑计算和量子计算、人工智能的关系

    2024-05-26 05:58:16       12 阅读
  11. 二叉树的链式实现

    2024-05-26 05:58:16       12 阅读
  12. node-nass安装踩坑

    2024-05-26 05:58:16       13 阅读
  13. 作业39 sqrt应用

    2024-05-26 05:58:16       16 阅读
  14. leetcode 409. 最长回文串

    2024-05-26 05:58:16       12 阅读
  15. JVM之回收策略的详细解析

    2024-05-26 05:58:16       11 阅读