当前位置:首页 > 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 datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…

vue实现mui

vue实现mui

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

vue实现验证

vue实现验证

Vue 表单验证实现方法 在Vue中实现表单验证可以通过多种方式完成,以下是常见的几种方法: 使用Vuelidate库 Vuelidate是一个轻量级的Vue表单验证库,安装后可以通过简单的配置实现…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

vue实现翻页

vue实现翻页

Vue实现翻页功能 在Vue中实现翻页功能通常需要结合分页组件和数据请求逻辑。以下是常见的实现方法: 使用Element UI分页组件 安装Element UI后,可以直接使用其分页组件:…

vue实现vmodel

vue实现vmodel

Vue 实现 v-model Vue 的 v-model 是一个语法糖,用于在表单元素或组件上实现双向数据绑定。以下是其实现方式和原理的详细说明。 在表单元素上使用 v-model v-model…