在vue中写一个小球做抛物线的动作,但是下降位置不对

在vue中写一个小球做抛物线的动作,但是下降位置不对

如下图1


                                         图1 

点击图1中的红色框中的“加入购物车”,出现图2中的现象


                         图2

小球下落的位置不对

该现象的food.vue的<template>如下图3所示


图3

.buy的css如下所示

.buy
      position: absolute
      right:18px
      bottom:18px
      z-index:10
      height:24px
      line-height:24px
      padding:0 12px
      box-sizing:border-box
      font-size:10px
      border-radius: 12px
      color: #ffffff
      background:rgb(0,160,220)


小球的触发时机是在show()函数里面定义的,如下所示

       //点击“加入购物车”按钮事件
        addFirst(event){
          if(!event._constructed){
            return;
          }
          //通过Vue.set()为对象设置属性,从而当该属性改变的时候,浏览器能够监听到该属性变化
          //然后触发相应的事件的执行
          //第一次点击的时候,this.food对象里面还没有属性count,所以需要通过Vue.set()设置
          Vue.set(this.food,'count',1);
          //通过this.$emit()将被点击的对象传给父组件
          console.log(event.target);
          this.$emit('add',event.target);
        }

通过console.log(event.target),在浏览器的console输出如下


显示display:none,因此获取不到小球刚开始的正确位置

解决方法,可以加入一个缓动的效果,在小球下降期间,这个div还没有设置成dixplay:none。即在该div外面加上一个transition,如下图所示:


.buy的css如下:

   .buy
      position: absolute
      right:18px
      bottom:18px
      z-index:10
      height:24px
      line-height:24px
      padding:0 12px
      box-sizing:border-box
      font-size:10px
      border-radius: 12px
      color: #ffffff
      background:rgb(0,160,220)
      opacity: 1
      &.fade-enter-active, &.fade-leave-active
        transition: all 0.2s
      &.fade-enter, &.fade-leave-active
        opacity: 0
        z-index: -1

加入缓动的效果,小球的动作就正确了










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Vue实现购物车小球抛物线动画效果的方法: 1. 首先,在Vue组件中定义一个数组,用于存储购物车中的商品信息。 2. 在购物车页面中,为每个商品添加一个按钮,点击按钮时触发一个方法,将该商品的信息添加到数组中。 3. 在页面中使用`v-for`指令遍历数组,将每个商品的信息渲染到页面上。 4. 在每个商品的元素上添加一个动画效果,使其在添加到购物车时产生抛物线运动的效果。 5. 在Vue组件中定义一个方法,用于计算抛物线的路径。该方法接收商品元素的位置信息和购物车元素的位置信息作为参数,根据这些信息计算出抛物线的路径。 6. 在添加商品到购物车的方法中,调用计算抛物线路径的方法,并将路径信息传递给动画效果。 7. 使用CSS动画或JavaScript动画库来实现抛物线动画效果。 下面是一个示例代码: ```html <template> <div> <div class="product" v-for="product in products" :key="product.id"> <img :src="product.image" alt="product image"> <button @click="addToCart(product)">Add to Cart</button> </div> <div class="cart"> <div class="ball" v-for="item in cartItems" :key="item.id" ref="ball"></div> </div> </div> </template> <script> export default { data() { return { products: [ { id: 1, image: 'product1.jpg' }, { id: 2, image: 'product2.jpg' }, { id: 3, image: 'product3.jpg' } ], cartItems: [] }; }, methods: { addToCart(product) { this.cartItems.push(product); this.animateBall(product); }, animateBall(product) { const ball = this.$refs.ball[this.cartItems.length - 1]; const productRect = product.$el.getBoundingClientRect(); const cartRect = this.$el.querySelector('.cart').getBoundingClientRect(); const startX = productRect.left + productRect.width / 2; const startY = productRect.top + productRect.height / 2; const endX = cartRect.left + cartRect.width / 2; const endY = cartRect.top + cartRect.height / 2; const curve = BezierEasing(0.42, 0, 0.58, 1); const duration = 1000; anime({ targets: ball, translateX: [startX, endX], translateY: [startY, endY], scale: [1, 0.5], opacity: [1, 0], easing: curve, duration: duration }); } } }; </script> <style> .product { display: inline-block; margin: 10px; } .cart { position: relative; width: 100px; height: 100px; background-color: #ccc; } .ball { position: absolute; width: 20px; height: 20px; background-color: red; border-radius: 50%; } </style> ``` 请注意,上述代码中的`product.image`和`product.$el`需要根据实际情况进行替换。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值