Vue项目中利用贝塞尔曲线实现添加购物车时的小球抛物线效果

文章目录


前言

大家好,我是尤雨海。。。。。。。。。


一、代码

<template>
  <div class="ball">
    <!-- 模拟商品区域 -->
    <ul>
      <li v-for="(item, index) in 5" :key="index">
        <span ref="add">+</span>
      </li>
    </ul>
    <!-- 模拟购物车区域 -->
    <div class="shopCar" ref="shopCar">{{ number }}</div>
  </div>
</template>

<script>
export default {
  name: "ball",
  data() {
    return {
      number: 0,
    };
  },
  mounted() {
    var buttons = this.$refs.add;
    for (let i = 0; i < buttons.length; i++) {
      buttons[i].addEventListener("click", this.addCar);
    }
  },

  methods: {
    /* 点击添加小球事件 */
    addCar(event) {
      this.number++;
      let x = event.pageX - event.target.offsetWidth / 2;
      let y = event.pageY - event.target.offsetWidth / 2;
      this.createBall(x, y);
    },

    /* 创建小球,添加动画 */
    createBall(left, top) {
      let bar = document.createElement("div");
      bar.style.position = "absolute";
      bar.style.left = left + "px";
      bar.style.top = top + "px";
      bar.style.width = "20px";
      bar.style.height = "20px";
      bar.style.borderRadius = "50%";
      bar.style.backgroundColor = "#02b6fd";
      bar.style.transition =
        "left .6s linear, top .6s cubic-bezier(0.5, -0.5, 1, 1)";

      document.body.appendChild(bar);
      // 添加动画属性
      setTimeout(() => {
        let target = this.$refs.shopCar;
        bar.style.left = target.offsetLeft + target.offsetWidth / 2 + "px";
        bar.style.top = target.offsetTop + "px";
      }, 0);

      /**
       * 动画结束后,删除自己
       */
      bar.ontransitionend = function () {
        this.remove();
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.ball {
  width: 100%;
  height: 100vh;
  ul {
    width: 100%;
    li {
      width: 100%;
      display: flex;
      justify-content: end;
      padding: 10px;
      span {
        display: inline-block;
        width: 30px;
        height: 30px;
        border-radius: 50%;
        background-color: aqua;
        color: aliceblue;
        font-size: 20px;
        text-align: center;
        line-height: 30px;
      }
    }
  }
}

.shopCar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: red;
  position: fixed;
  left: 20px;
  bottom: 30px;
  text-align: center;
  line-height: 50px;
  color: white;
}
</style>



  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值