当前位置:首页 > VUE

vue实现跑车

2026-02-09 23:29:26VUE

Vue 实现跑车动画

使用 Vue 结合 CSS 动画可以实现一个简单的跑车动画效果。以下是一个完整的实现示例:

创建 Vue 组件

<template>
  <div class="road">
    <div class="car" :style="{ left: carPosition + 'px' }"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      carPosition: 0,
      animationId: null
    }
  },
  mounted() {
    this.animateCar()
  },
  beforeUnmount() {
    cancelAnimationFrame(this.animationId)
  },
  methods: {
    animateCar() {
      const speed = 3
      this.carPosition += speed

      if (this.carPosition > window.innerWidth) {
        this.carPosition = -100
      }

      this.animationId = requestAnimationFrame(this.animateCar)
    }
  }
}
</script>

添加 CSS 样式

.road {
  position: relative;
  width: 100%;
  height: 100px;
  background-color: #555;
  overflow: hidden;
}

.car {
  position: absolute;
  width: 100px;
  height: 50px;
  background-color: red;
  bottom: 0;
  border-radius: 10px 10px 0 0;
}

.car::before,
.car::after {
  content: '';
  position: absolute;
  width: 30px;
  height: 30px;
  background-color: #333;
  border-radius: 50%;
  bottom: -15px;
}

.car::before {
  left: 10px;
}

.car::after {
  right: 10px;
}

更高级的实现

如果需要更真实的跑车效果,可以使用 SVG 或图片:

<template>
  <div class="road">
    <img 
      class="car" 
      src="https://example.com/car.png" 
      :style="{ 
        left: carPosition + 'px',
        transform: `scaleX(${direction})`
      }"
    >
  </div>
</template>

<script>
export default {
  data() {
    return {
      carPosition: 0,
      direction: 1,
      animationId: null
    }
  },
  methods: {
    animateCar() {
      this.carPosition += 3 * this.direction

      if (this.carPosition > window.innerWidth) {
        this.direction = -1
      } else if (this.carPosition < 0) {
        this.direction = 1
      }

      this.animationId = requestAnimationFrame(this.animateCar)
    }
  }
}
</script>

使用第三方动画库

对于更复杂的动画效果,可以考虑使用 GSAP 或 Anime.js:

vue实现跑车

import gsap from 'gsap'

methods: {
  animateWithGSAP() {
    gsap.to(this.$refs.car, {
      x: window.innerWidth,
      duration: 5,
      ease: "power1.inOut",
      onComplete: () => {
        gsap.to(this.$refs.car, {
          x: 0,
          duration: 5,
          ease: "power1.inOut"
        })
      }
    })
  }
}

注意事项

  • 使用 requestAnimationFrame 可以获得更流畅的动画效果
  • 在组件销毁时记得取消动画帧请求
  • 对于移动端需要考虑性能优化
  • 可以使用 CSS transform 代替直接修改位置属性以获得更好的性能

标签: 跑车vue
分享给朋友:

相关文章

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组件中引入…

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue 实现筛选

vue 实现筛选

实现筛选功能的基本方法 在Vue中实现筛选功能通常涉及以下几个核心步骤: 数据绑定与筛选逻辑 使用v-model绑定筛选条件到Vue实例的数据属性,结合计算属性实现动态筛选: data() {…

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…