当前位置:首页 > VUE

vue实现轮播滚动显示

2026-02-23 05:53:57VUE

使用Swiper插件实现轮播

Swiper是一个流行的开源触摸滑动插件,专为移动设备优化,同时兼容PC端。在Vue项目中可以通过swiper/vue包快速集成。

安装依赖:

npm install swiper vue-awesome-swiper

基础实现代码:

<template>
  <swiper
    :modules="modules"
    :slides-per-view="3"
    :space-between="50"
    :autoplay="{ delay: 2500 }"
    @swiper="onSwiper"
  >
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image" />
    </swiper-slide>
  </swiper>
</template>

<script>
import { Autoplay } from 'swiper'
import { Swiper, SwiperSlide } from 'swiper/vue'

export default {
  components: { Swiper, SwiperSlide },
  setup() {
    return {
      modules: [Autoplay],
      slides: [
        { image: 'image1.jpg' },
        { image: 'image2.jpg' },
        { image: 'image3.jpg' }
      ]
    }
  }
}
</script>

纯CSS实现轮播效果

通过CSS动画和overflow-hidden容器可以实现基础轮播效果,适合轻量级需求。

<template>
  <div class="carousel-container">
    <div class="carousel-track" :style="trackStyle">
      <div v-for="(item, index) in items" :key="index" class="slide">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentIndex: 0,
      items: [
        { content: 'Slide 1' },
        { content: 'Slide 2' },
        { content: 'Slide 3' }
      ]
    }
  },
  computed: {
    trackStyle() {
      return {
        transform: `translateX(-${this.currentIndex * 100}%)`,
        transition: 'transform 0.5s ease'
      }
    }
  },
  mounted() {
    setInterval(() => {
      this.currentIndex = (this.currentIndex + 1) % this.items.length
    }, 3000)
  }
}
</script>

<style>
.carousel-container {
  width: 100%;
  overflow: hidden;
}
.carousel-track {
  display: flex;
  width: 300%;
}
.slide {
  flex: 1;
  min-width: 33.33%;
}
</style>

使用Vue Transition组件

结合Vue的过渡系统实现更丰富的动画效果。

<template>
  <div class="carousel">
    <transition-group name="slide" tag="div">
      <div v-for="(item, index) in visibleItems" :key="item.id" class="item">
        {{ item.text }}
      </div>
    </transition-group>
    <button @click="next">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' }
      ],
      currentIndex: 0
    }
  },
  computed: {
    visibleItems() {
      return [this.items[this.currentIndex]]
    }
  },
  methods: {
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.items.length
    }
  },
  mounted() {
    setInterval(this.next, 2000)
  }
}
</script>

<style>
.slide-enter-active, .slide-leave-active {
  transition: all 0.5s ease;
}
.slide-enter-from {
  transform: translateX(100%);
}
.slide-leave-to {
  transform: translateX(-100%);
}
</style>

响应式轮播实现

根据不同屏幕尺寸调整显示数量,需要结合ResizeObserver API。

<template>
  <div ref="container" class="carousel">
    <div class="slides" :style="slidesStyle">
      <div v-for="(item, index) in items" :key="index" class="slide">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Slide 1', 'Slide 2', 'Slide 3', 'Slide 4'],
      visibleCount: 1,
      containerWidth: 0
    }
  },
  mounted() {
    this.updateVisibleCount()
    const observer = new ResizeObserver(this.updateVisibleCount)
    observer.observe(this.$refs.container)
  },
  methods: {
    updateVisibleCount() {
      const width = this.$refs.container.clientWidth
      this.visibleCount = width > 768 ? 3 : width > 480 ? 2 : 1
      this.containerWidth = width
    }
  },
  computed: {
    slidesStyle() {
      return {
        width: `${(this.items.length / this.visibleCount) * 100}%`,
        transform: `translateX(-${this.currentIndex * (100 / this.items.length)}%)`
      }
    }
  }
}
</script>

vue实现轮播滚动显示

标签: vue
分享给朋友:

相关文章

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue实现波形

vue实现波形

实现波形效果的方法 在Vue中实现波形效果可以通过多种方式完成,常见的方法包括使用Canvas绘制、CSS动画或第三方库。以下是几种实现方案: 使用Canvas绘制波形 通过Canvas API动…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局…

vue 实现图钉

vue 实现图钉

实现图钉效果的步骤 在Vue中实现图钉(固定位置)效果,可以通过CSS的position: sticky属性或监听滚动事件动态修改元素样式。以下是两种常见方法: 方法一:使用CSS的positio…