当前位置:首页 > VUE

vue实现纵向轮播

2026-03-08 09:04:23VUE

Vue 实现纵向轮播

纵向轮播可以通过 CSS 动画和 Vue 的动态数据绑定实现。以下是一个基于 Vue 3 的实现方案。

基本结构

使用 Vue 的 transition-group 和 CSS 过渡效果实现纵向滚动效果。

<template>
  <div class="carousel-container">
    <div class="carousel-wrapper" ref="wrapper">
      <transition-group name="slide" tag="div">
        <div class="carousel-item" v-for="(item, index) in items" :key="index">
          {{ item }}
        </div>
      </transition-group>
    </div>
  </div>
</template>

数据与样式

定义轮播数据项和基本样式,确保内容纵向排列。

<script>
export default {
  data() {
    return {
      items: ["Item 1", "Item 2", "Item 3", "Item 4"],
      currentIndex: 0,
    };
  },
};
</script>

<style>
.carousel-container {
  height: 200px;
  overflow: hidden;
  position: relative;
}

.carousel-wrapper {
  height: 100%;
}

.carousel-item {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f0f0;
  margin-bottom: 10px;
}

.slide-enter-active,
.slide-leave-active {
  transition: all 0.5s ease;
}

.slide-enter-from {
  transform: translateY(100%);
}

.slide-leave-to {
  transform: translateY(-100%);
}
</style>

自动轮播逻辑

通过 setInterval 实现自动轮播,动态更新 currentIndex 并处理边界条件。

<script>
export default {
  data() {
    return {
      items: ["Item 1", "Item 2", "Item 3", "Item 4"],
      currentIndex: 0,
      interval: null,
    };
  },
  mounted() {
    this.startAutoPlay();
  },
  beforeUnmount() {
    this.stopAutoPlay();
  },
  methods: {
    startAutoPlay() {
      this.interval = setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.items.length;
      }, 3000);
    },
    stopAutoPlay() {
      clearInterval(this.interval);
    },
  },
};
</script>

动态绑定

currentIndex 与轮播项的位置绑定,通过 CSS 控制偏移实现平滑滚动。

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

<style>
.carousel-inner {
  transition: transform 0.5s ease;
}
</style>

手势支持

添加触摸事件支持,提升移动端体验。

<script>
export default {
  methods: {
    handleTouchStart(e) {
      this.startY = e.touches[0].clientY;
    },
    handleTouchMove(e) {
      this.deltaY = e.touches[0].clientY - this.startY;
    },
    handleTouchEnd() {
      if (this.deltaY > 50) {
        this.currentIndex = Math.max(0, this.currentIndex - 1);
      } else if (this.deltaY < -50) {
        this.currentIndex = Math.min(this.items.length - 1, this.currentIndex + 1);
      }
      this.deltaY = 0;
    },
  },
};
</script>

完整示例

整合以上功能,实现完整的纵向轮播组件。

vue实现纵向轮播

<template>
  <div
    class="carousel-container"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    <div class="carousel-wrapper" ref="wrapper">
      <div
        class="carousel-inner"
        :style="{ transform: `translateY(-${currentIndex * 100}%)` }"
      >
        <div class="carousel-item" v-for="(item, index) in items" :key="index">
          {{ item }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ["Item 1", "Item 2", "Item 3", "Item 4"],
      currentIndex: 0,
      interval: null,
      startY: 0,
      deltaY: 0,
    };
  },
  mounted() {
    this.startAutoPlay();
  },
  beforeUnmount() {
    this.stopAutoPlay();
  },
  methods: {
    startAutoPlay() {
      this.interval = setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.items.length;
      }, 3000);
    },
    stopAutoPlay() {
      clearInterval(this.interval);
    },
    handleTouchStart(e) {
      this.startY = e.touches[0].clientY;
    },
    handleTouchMove(e) {
      this.deltaY = e.touches[0].clientY - this.startY;
    },
    handleTouchEnd() {
      if (this.deltaY > 50) {
        this.currentIndex = Math.max(0, this.currentIndex - 1);
      } else if (this.deltaY < -50) {
        this.currentIndex = Math.min(this.items.length - 1, this.currentIndex + 1);
      }
      this.deltaY = 0;
    },
  },
};
</script>

<style>
.carousel-container {
  height: 200px;
  overflow: hidden;
  position: relative;
}

.carousel-wrapper {
  height: 100%;
}

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

.carousel-item {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f0f0;
}
</style>

标签: 纵向vue
分享给朋友:

相关文章

vue 实现滚动

vue 实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以通…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue 实现表单

vue 实现表单

Vue 表单实现方法 Vue.js 提供了多种方式实现表单,包括双向数据绑定、表单验证和动态表单生成。以下是常见的实现方法: 基础表单绑定 使用 v-model 指令实现表单元素与数据的双向绑定:…

vue实现ios

vue实现ios

Vue 实现 iOS 风格应用 使用 Vue 实现 iOS 风格的应用,可以通过结合 UI 框架和自定义样式来达成目标。以下是具体方法和步骤: 选择 iOS 风格的 UI 框架 Vue 生态中有多…

vue实现京东

vue实现京东

Vue 实现京东电商网站的关键步骤 项目搭建与基础配置 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理路由,Vuex/Pinia 管理状态。配置基础 UI 框架如 E…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…