实现 图片 多帧 移出移入动效

 <div class="img"
      :style="{ 'background-position-y': currentValue + 'px', 'background-image': ' url(https://img.alicdn.com/imgextra/i1/O1CN01nYkaXy1KudUZDMbYJ_!!6000000001224-2-tps-240-4080.png)' }"
      @mouseout="mouseentera" @mouseover="mouseovera"></div>
 data() {
    return {
      currentValue: 0, // 当前值
      startValue: 0, // 起始值
      endValue: -1920, // 结束值
      duration: 480, // 动画持续时间(毫秒)
      intervalTime: 10, // 每次动画间隔时间(毫秒)
      startTime: null, // 开始时间
      timer: null, // 计时器
      stepCount: 0, // 步数计数器
    }
  },
  methods: {
    startAnimation() {
      this.startTime = Date.now();
      this.timer = setInterval(this.updateAnimation, this.intervalTime);
    },
    updateAnimation() {
      const currentTime = Date.now();
      const progress = currentTime - this.startTime;
      const stepDuration = this.duration / 16; // 每个步骤的持续时间
      const stepValue = (this.endValue - this.startValue) / 16; // 每个步骤的值变化量
      if (progress >= this.duration) {
        clearInterval(this.timer);
        this.currentValue = this.endValue;
        return;
      }

      this.stepCount = Math.floor(progress / stepDuration);
      this.currentValue = this.startValue + this.stepCount * stepValue;
      console.log('this.currentValue :>> ', this.currentValue);

    },
    mouseovera(){
      console.log('object :>> 移入');
      this.state=true
      this.startValue=0
      this.endValue = -1920
      this.startAnimation()
    },
    mouseentera() {
      console.log('object :>> 移出');
      this.startValue=-1920
      this.endValue = 0
      this.startAnimation()

    }
  }
.img {
  height: 120px;
  width: 120px;
  margin-bottom: 8px;
  background-size: cover;
  margin-left: 32px;
  background-repeat: no-repeat;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值