当前位置:首页 > VUE

vue实现跑道滚动

2026-03-28 17:56:19VUE

vue实现跑道滚动效果

跑道滚动效果常见于轮播图或横向滚动展示场景,以下是几种实现方法:

使用CSS动画实现基础滚动

<template>
  <div class="track-container">
    <div class="track" :style="{ animationDuration: duration + 's' }">
      <div v-for="(item, index) in items" :key="index" class="item">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [1, 2, 3, 4, 5],
      duration: 10
    }
  }
}
</script>

<style scoped>
.track-container {
  overflow: hidden;
  width: 100%;
}

.track {
  display: flex;
  animation: scroll linear infinite;
}

@keyframes scroll {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(-100%);
  }
}

.item {
  flex-shrink: 0;
  width: 200px;
  height: 100px;
  margin-right: 20px;
  background: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>

使用requestAnimationFrame实现高性能滚动

vue实现跑道滚动

export default {
  data() {
    return {
      items: [1, 2, 3, 4, 5],
      position: 0,
      speed: 1,
      animationId: null
    }
  },
  mounted() {
    this.startAnimation()
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId)
  },
  methods: {
    startAnimation() {
      const animate = () => {
        this.position -= this.speed
        if (Math.abs(this.position) >= this.$refs.track.scrollWidth / 2) {
          this.position = 0
        }
        this.$refs.track.style.transform = `translateX(${this.position}px)`
        this.animationId = requestAnimationFrame(animate)
      }
      animate()
    }
  }
}

无限循环滚动的优化版本

<template>
  <div class="track-wrapper">
    <div class="track" ref="track">
      <div v-for="item in displayItems" :key="item.id" class="item">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, content: 'Item 1' },
        { id: 2, content: 'Item 2' },
        { id: 3, content: 'Item 3' }
      ],
      displayItems: []
    }
  },
  mounted() {
    this.duplicateItems()
    this.startScroll()
  },
  methods: {
    duplicateItems() {
      this.displayItems = [...this.items, ...this.items]
    },
    startScroll() {
      // 实现滚动逻辑
    }
  }
}
</script>

使用第三方库实现

vue实现跑道滚动

可以考虑使用swiper.js等成熟轮播库:

import Swiper from 'swiper'
import 'swiper/css/swiper.css'

export default {
  mounted() {
    new Swiper('.swiper-container', {
      loop: true,
      autoplay: {
        delay: 2500,
        disableOnInteraction: false
      },
      slidesPerView: 'auto',
      spaceBetween: 30
    })
  }
}

响应式处理

添加窗口大小变化的响应处理:

export default {
  data() {
    return {
      itemWidth: 200
    }
  },
  mounted() {
    window.addEventListener('resize', this.handleResize)
    this.handleResize()
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
  },
  methods: {
    handleResize() {
      this.itemWidth = window.innerWidth > 768 ? 200 : 150
    }
  }
}

以上方法可以根据实际需求选择,CSS动画适合简单场景,requestAnimationFrame提供更精细控制,第三方库则提供开箱即用的完整解决方案。

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

相关文章

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue实现watch

vue实现watch

监听数据变化 在Vue中,watch用于监听响应式数据的变化并执行回调函数。基本语法如下: watch: { propertyName(newValue, oldValue) { //…

vue实现刷新

vue实现刷新

Vue 实现页面刷新的方法 在Vue中实现页面刷新可以通过多种方式,以下是几种常见的方法: 使用 window.location.reload() 通过调用浏览器的原生方法强制刷新当前页面:…

vue实现修改

vue实现修改

Vue 实现修改功能的方法 在 Vue 中实现修改功能通常涉及表单绑定、状态管理和 API 调用。以下是几种常见实现方式: 双向数据绑定实现表单修改 使用 v-model 指令绑定表单元素与组件数据…