当前位置:首页 > VUE

vue实现字体垂直轮播

2026-02-21 19:27:30VUE

Vue 实现字体垂直轮播的方法

垂直轮播效果通常用于展示公告、新闻标题等。以下是几种实现方式:

使用 CSS 动画实现

通过 Vue 控制 CSS 动画实现简单的垂直轮播效果:

<template>
  <div class="vertical-carousel">
    <div class="carousel-list" :style="{ transform: `translateY(${translateY}px)` }">
      <div v-for="(item, index) in items" :key="index" class="carousel-item">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['公告1', '公告2', '公告3', '公告4'],
      currentIndex: 0,
      translateY: 0,
      itemHeight: 40 // 根据实际项目高度调整
    }
  },
  mounted() {
    this.startCarousel()
  },
  methods: {
    startCarousel() {
      setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.items.length
        this.translateY = -this.currentIndex * this.itemHeight
      }, 2000)
    }
  }
}
</script>

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

使用第三方库

对于更复杂的需求,可以使用 vue-awesome-swiper 等轮播库:

  1. 安装依赖:

    npm install swiper vue-awesome-swiper --save
  2. 实现代码:

    
    <template>
    <swiper :options="swiperOption" direction="vertical" class="vertical-swiper">
     <swiper-slide v-for="(item, index) in items" :key="index">
       {{ item }}
     </swiper-slide>
    </swiper>
    </template>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper' import 'swiper/css/swiper.css'

export default { components: { Swiper, SwiperSlide }, data() { return { items: ['消息1', '消息2', '消息3', '消息4'], swiperOption: { direction: 'vertical', autoplay: { delay: 2000, disableOnInteraction: false }, loop: true, slidesPerView: 1, spaceBetween: 0 } } } }

.vertical-swiper { height: 40px; } ```

无限循环无缝轮播

实现无缝轮播效果,避免跳转时的突兀感:

vue实现字体垂直轮播

<template>
  <div class="seamless-carousel" ref="carousel">
    <div class="carousel-content">
      <div v-for="(item, index) in extendedItems" 
           :key="index" 
           class="carousel-item"
           :class="{ active: index === currentIndex }">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['新闻1', '新闻2', '新闻3'],
      currentIndex: 0,
      extendedItems: []
    }
  },
  created() {
    this.extendedItems = [...this.items, this.items[0]]
  },
  mounted() {
    this.startCarousel()
  },
  methods: {
    startCarousel() {
      setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.extendedItems.length

        if (this.currentIndex === this.items.length) {
          setTimeout(() => {
            this.currentIndex = 0
          }, 500)
        }
      }, 2000)
    }
  }
}
</script>

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

注意事项

  1. 轮播高度需要根据实际内容调整
  2. 移动端需要考虑触摸事件处理
  3. 性能优化:组件销毁时清除定时器
  4. 内容更新时需要重置轮播状态
  5. 对于大量数据考虑虚拟滚动技术

以上方法可以根据实际需求进行调整和组合,实现更复杂的垂直轮播效果。

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

相关文章

vue使用vr实现标注

vue使用vr实现标注

Vue 中使用 VR 实现标注的方法 在 Vue 项目中结合 VR 技术实现标注功能,可以通过 WebXR 和 Three.js 等库实现。以下为具体实现方法: 安装依赖 确保项目中已安装 Thre…

vue实现画圆弧并着色

vue实现画圆弧并着色

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

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…

vue实现发帖

vue实现发帖

使用 Vue 实现发帖功能 前端实现 创建发帖表单组件,包含标题和内容输入框,以及提交按钮。使用 v-model 绑定表单数据。 <template> <div>…

vue实现模糊

vue实现模糊

Vue实现模糊搜索的方法 在Vue中实现模糊搜索功能通常需要结合输入框和列表渲染,通过监听输入内容动态过滤数据。以下是几种常见实现方式: 使用计算属性实现 计算属性适合处理需要响应式更新的搜索逻辑:…