微信小程序实现 给每一个元素对象item做动画,并且相互之间不影响实现

在这里插入图片描述在这里插入图片描述
如图所示,实现点击切换展开与闭合。小三角加上旋转的动画,并且item之间互相不影响

<view class='i-bottom' bindtap='showDetail' data-id="{{index}}" >
     <view>点击查看办理详情</view>
     <view class="arrow" animation="{{animationList[index]}}"></view>
</view>

谁要做动画,就把动画属性animation加到谁的身上

data{
	animationList: [], // 存放动画的序列数组
},
	// 用户点击的时候做动画
  showDetail(e) {
    const that = this
    let index = e.currentTarget.dataset.id
    let animationList = this.data.animationList
    // 创建动画实例,调用实例的方法来描述动画。最后通过动画实例的 export 方法导出动画数据传递给组件的 animation 属性。     做动画  将来可以封装一个方法去调用
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease',
      delay: 0
    })
    // console.log(animation) // 动画实例animation   对象中有一个属性 currentTransform 里面有个属性值 rotate 是后来我们自己加上去的
    this.animation = animation // 将动画挂载到全局中去
    if (!that.data.cardItem[index].isTrue) { // 如果当前用户点击的元素的 isTrue值为false (默认就是false)
      this.animation.rotate(225).step() // 给动画对象 挂载 旋转动画属性
    } else {
      this.animation.rotate(45).step()
    }
    animationList[index] = animation.export() // 将当前的动画对象数据序列放入animationList数组中  export方法每次调用后会清掉之前的动画操作
    let isTrue = "cardItem[" + index + "].isTrue" //获取当前点击的数组中对象的值   // cardItem[1].isTrue
    // console.log(isTrue)
    this.setData({
      animationList: animationList // 修改新的动画数组
    })
    setTimeout(function() {
      that.setData({
        [isTrue]: !that.data.cardItem[index].isTrue, 
      })
    }, 200)
  },

终于完成。还是需要多逛逛官方文档,多理解。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值