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

vue实现轮播滚动显示

<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 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue实现vnc

vue实现vnc

Vue 实现 VNC 客户端 在 Vue 项目中实现 VNC 客户端功能,可以通过集成现有的 VNC 客户端库或组件完成。以下是具体实现方法: 安装依赖库 使用 noVNC 或其他 VNC 客户端…

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…

vue实现监听

vue实现监听

监听数据变化 在Vue中,可以通过watch选项或$watch方法监听数据的变化。watch适用于组件选项内声明式监听,$watch适用于动态监听。 // 选项式API export default…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…