当前位置:首页 > VUE

vue 实现轮播文字

2026-01-18 21:10:02VUE

使用 Swiper 插件实现轮播文字

安装 Swiper 依赖:

npm install swiper

在 Vue 组件中引入 Swiper:

<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(text, index) in texts" :key="index">
        {{ text }}
      </div>
    </div>
  </div>
</template>

<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css'

export default {
  data() {
    return {
      texts: ['文字1', '文字2', '文字3']
    }
  },
  mounted() {
    new Swiper('.swiper-container', {
      direction: 'vertical', // 垂直方向滚动
      loop: true, // 循环播放
      autoplay: {
        delay: 2000, // 2秒切换一次
        disableOnInteraction: false
      }
    })
  }
}
</script>

<style>
.swiper-container {
  height: 40px;
  overflow: hidden;
}
</style>

使用 CSS 动画实现简单轮播

<template>
  <div class="text-carousel">
    <div class="text-container" :style="{ transform: `translateY(${-currentIndex * 100}%)` }">
      <div class="text-item" v-for="(text, index) in texts" :key="index">
        {{ text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      texts: ['公告1', '公告2', '公告3'],
      currentIndex: 0,
      interval: null
    }
  },
  mounted() {
    this.startCarousel()
  },
  beforeDestroy() {
    clearInterval(this.interval)
  },
  methods: {
    startCarousel() {
      this.interval = setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.texts.length
      }, 2000)
    }
  }
}
</script>

<style>
.text-carousel {
  height: 40px;
  overflow: hidden;
  position: relative;
}
.text-container {
  transition: transform 0.5s ease;
}
.text-item {
  height: 40px;
  line-height: 40px;
}
</style>

使用 Vue Transition 组件实现

<template>
  <div class="transition-carousel">
    <transition name="slide" mode="out-in">
      <div :key="currentText" class="text-item">
        {{ currentText }}
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      texts: ['消息1', '消息2', '消息3'],
      currentIndex: 0,
      interval: null
    }
  },
  computed: {
    currentText() {
      return this.texts[this.currentIndex]
    }
  },
  mounted() {
    this.startCarousel()
  },
  beforeDestroy() {
    clearInterval(this.interval)
  },
  methods: {
    startCarousel() {
      this.interval = setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.texts.length
      }, 2000)
    }
  }
}
</script>

<style>
.transition-carousel {
  height: 40px;
  overflow: hidden;
  position: relative;
}
.text-item {
  height: 40px;
  line-height: 40px;
}
.slide-enter-active, .slide-leave-active {
  transition: all 0.5s ease;
}
.slide-enter {
  transform: translateY(40px);
  opacity: 0;
}
.slide-leave-to {
  transform: translateY(-40px);
  opacity: 0;
}
</style>

使用第三方组件 vue-awesome-swiper

安装组件:

npm install vue-awesome-swiper swiper@5.4.5

实现代码:

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(text, index) in texts" :key="index">
      {{ text }}
    </swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      texts: ['轮播内容1', '轮播内容2', '轮播内容3'],
      swiperOption: {
        direction: 'vertical',
        loop: true,
        autoplay: {
          delay: 2000,
          disableOnInteraction: false
        }
      }
    }
  }
}
</script>

<style>
.swiper-container {
  height: 40px;
}
</style>

以上方法可以根据项目需求选择适合的实现方式,Swiper 插件功能最全面但体积较大,CSS 动画实现最轻量但功能简单,vue-awesome-swiper 提供了 Vue 专用的封装接口。

vue 实现轮播文字

标签: 文字vue
分享给朋友:

相关文章

vue实现年历

vue实现年历

Vue 实现年历的方法 使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式: 基于原生 Vue 的日历组件 创建 Calendar.vue 组件文件…

vue实现检测数组

vue实现检测数组

Vue 中检测数组变化的方法 Vue 的响应式系统无法直接检测到数组的某些变化,因为 JavaScript 的限制。以下是一些解决方案: 使用 Vue.set 或 this.$set Vue 提供…

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue实现字母添加排序

vue实现字母添加排序

实现字母添加排序的方法 在Vue中实现字母添加排序功能,可以通过以下步骤完成: 创建Vue实例并定义数据 new Vue({ el: '#app', data: { lette…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…