当前位置:首页 > VUE

vue点击实现文字轮播

2026-02-21 02:27:54VUE

Vue 实现文字轮播

使用 CSS 动画实现

通过 CSS 的 animation@keyframes 实现文字轮播效果,适用于简单的文字切换。

<template>
  <div class="text-carousel">
    <div class="text-content">
      {{ currentText }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      texts: ['文本1', '文本2', '文本3'],
      currentIndex: 0
    };
  },
  computed: {
    currentText() {
      return this.texts[this.currentIndex];
    }
  },
  mounted() {
    setInterval(() => {
      this.currentIndex = (this.currentIndex + 1) % this.texts.length;
    }, 2000);
  }
};
</script>

<style>
.text-carousel {
  overflow: hidden;
  height: 30px;
}

.text-content {
  animation: slide 2s infinite;
}

@keyframes slide {
  0% { transform: translateY(0); opacity: 1; }
  50% { transform: translateY(-30px); opacity: 0; }
  100% { transform: translateY(0); opacity: 1; }
}
</style>

使用 Vue Transition 组件

通过 Vue 的 <transition> 组件实现平滑的过渡效果。

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

<script>
export default {
  data() {
    return {
      texts: ['文本1', '文本2', '文本3'],
      currentIndex: 0
    };
  },
  computed: {
    currentText() {
      return this.texts[this.currentIndex];
    }
  },
  mounted() {
    setInterval(() => {
      this.currentIndex = (this.currentIndex + 1) % this.texts.length;
    }, 2000);
  }
};
</script>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

使用第三方库

使用 vue-carousel 等第三方库快速实现轮播效果。

安装依赖:

npm install vue-carousel

代码示例:

<template>
  <carousel :autoplay="true" :loop="true" :autoplayTimeout="2000">
    <slide v-for="(text, index) in texts" :key="index">
      {{ text }}
    </slide>
  </carousel>
</template>

<script>
import { Carousel, Slide } from 'vue-carousel';

export default {
  components: { Carousel, Slide },
  data() {
    return {
      texts: ['文本1', '文本2', '文本3']
    };
  }
};
</script>

点击切换文字

通过点击事件手动切换轮播文字。

vue点击实现文字轮播

<template>
  <div class="text-carousel" @click="nextText">
    <div class="text-content">
      {{ currentText }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      texts: ['文本1', '文本2', '文本3'],
      currentIndex: 0
    };
  },
  computed: {
    currentText() {
      return this.texts[this.currentIndex];
    }
  },
  methods: {
    nextText() {
      this.currentIndex = (this.currentIndex + 1) % this.texts.length;
    }
  }
};
</script>

<style>
.text-carousel {
  cursor: pointer;
}
</style>

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

相关文章

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: npm i…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

vue实现必填

vue实现必填

Vue 实现表单必填验证的方法 在Vue中实现表单必填验证可以通过多种方式完成,以下是几种常见的方法: 使用HTML5原生属性 最简单的必填验证可以通过HTML5的required属性实现: &l…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用 n…