当前位置:首页 > VUE

vue实现字体垂直轮播

2026-01-21 03:56:59VUE

实现思路

垂直轮播效果可以通过动态改变字体容器的 transformmargin-top 属性来实现。使用 Vue 的过渡动画和定时器控制轮播节奏。

基本结构

创建包含轮播内容的容器,使用 v-for 渲染列表,通过 CSS 控制垂直排列和隐藏溢出部分。

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

数据与样式

定义轮播数据和基础样式,确保容器高度固定且溢出隐藏。

<script>
export default {
  data() {
    return {
      items: [
        { text: "第一项内容" },
        { text: "第二项内容" },
        { text: "第三项内容" }
      ],
      currentIndex: 0,
      offset: 0,
      itemHeight: 40 // 根据实际样式调整
    };
  }
};
</script>

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

自动轮播逻辑

通过 setInterval 控制轮播,更新 offset 实现滚动效果。注意组件销毁时清除定时器。

<script>
export default {
  mounted() {
    this.startCarousel();
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  methods: {
    startCarousel() {
      this.timer = setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.items.length;
        this.offset = -this.currentIndex * this.itemHeight;
      }, 2000);
    }
  }
};
</script>

平滑过渡优化

添加过渡效果,确保轮播时内容平滑移动。可通过 CSS 的 transition 属性实现。

.carousel-container {
  transition: transform 0.5s ease;
}

无限循环处理

克隆首项数据至末尾,实现无缝循环。需在滚动到末尾时重置位置。

<script>
export default {
  data() {
    return {
      items: [
        { text: "第一项内容" },
        { text: "第二项内容" },
        { text: "第三项内容" },
        { text: "第一项内容" } // 克隆首项
      ],
      itemCount: 3 // 实际项数
    };
  },
  methods: {
    startCarousel() {
      this.timer = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex >= this.itemCount) {
          setTimeout(() => {
            this.currentIndex = 0;
            this.offset = 0;
          }, 500); // 过渡完成后重置
        }
        this.offset = -this.currentIndex * this.itemHeight;
      }, 2000);
    }
  }
};
</script>

手势支持(可选)

添加 touchstarttouchmove 事件实现手动滑动交互,需计算滑动距离与方向。

<script>
export default {
  methods: {
    handleTouchStart(e) {
      this.startY = e.touches[0].clientY;
    },
    handleTouchMove(e) {
      const currentY = e.touches[0].clientY;
      this.deltaY = currentY - this.startY;
      this.offset = -this.currentIndex * this.itemHeight + this.deltaY;
    },
    handleTouchEnd() {
      if (Math.abs(this.deltaY) > this.itemHeight / 2) {
        this.currentIndex += this.deltaY > 0 ? -1 : 1;
      }
      this.offset = -this.currentIndex * this.itemHeight;
    }
  }
};
</script>

响应式调整

监听容器高度变化,动态计算 itemHeight。可使用 ResizeObserver 或窗口大小事件。

<script>
export default {
  mounted() {
    this.updateItemHeight();
    window.addEventListener('resize', this.updateItemHeight);
  },
  methods: {
    updateItemHeight() {
      this.itemHeight = this.$el.clientHeight;
    }
  }
};
</script>

以上方法组合可实现一个完整的垂直字体轮播效果,支持自动播放、平滑过渡和手动交互。

vue实现字体垂直轮播

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

相关文章

vue中如何实现循环

vue中如何实现循环

在 Vue 中实现循环 Vue 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法为…

vue实现签约合作弹窗

vue实现签约合作弹窗

实现签约合作弹窗的步骤 使用Vue实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDi…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Des…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <di…