uni-app 小程序 实现添加购物车抛物线掉入效果

总体思路其实跟H5的无差;

  1. 获取当前抛物线起始位置的坐标
  2. 获取购物车图标的坐标
  3. 通过创建一个 图标从 起始坐标点到终止坐标点 的动画效果(延时一秒),这样就造成一个抛物线的假象了。

上代码:

其中 animationElStatus 是需要动态计算起始及终止坐标点位置后的 动态样式。

此html样式,会在每次点击加入购物车时,动态写入style后触发,将img掉入购物车位置
注意这里的变量animationElStatus,使用模板字符串连接时,不能折行,不能忘记分号隔开样式
this.animationElStatus = top: ${endPoint.top}px; left: ${endPoint.left}px; transition: all 1s; transform: rotate(-720deg);

 <view
   id="animation-el"
   v-show="animationElStatus"
   :style="animationElStatus"
 >
   <img :src="`${cdnImageUrl}/privateMall/icon/icon_add.png`" />
 </view>

endPoint记录当前终点坐标位置
uni.createSelectorQuery().select(‘#animation-end’).boundingClientRect 形如:document.getElementById()

let endPoint = null; // 动画截止点
mounted() {
  // 提前获取最下方购物车位置
  if (!endPoint) {
    uni.createSelectorQuery().select('#animation-end').boundingClientRect((res) => {
      endPoint = res
    }).exec()
  }
},

点击商品触发下列方法,通过传入当前起始坐标点
click传入的点击事件坐标,在detail中。

   // 通过click事件记录起始点位置 子组件
   toggleCheck(ev) {
      const { x, y } = ev.detail
      this.$emit('animationAddCart', {
        x,
        y
      })
    }
    
    /**
     * 商品掉入动画方法 父组件
     */
    animationAddCart(position) {
      if (!endPoint) return

      this.animationElStatus = `top: ${position.y}px; left: ${position.x}px;`

      setTimeout(() => {
        this.animationElStatus = `top: ${endPoint.top}px; left: ${endPoint.left}px; transition: all 1s; transform: rotate(-720deg);`
      }, 20);

      setTimeout(() => {
        this.animationElStatus = ''
      }, 1000);
    },
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值