当前位置:首页 > 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 组件的实现方式 Vue 组件可以通过多种方式实现,以下是常见的几种方法: 单文件组件 (SFC) 使用 .vue 文件,包含模板、脚本和样式三部分: <template>…

vue实现飘窗原理

vue实现飘窗原理

vue实现飘窗原理 飘窗效果通常指在页面上悬浮显示的弹窗或通知,跟随页面滚动或固定在某个位置。Vue实现这一效果主要依赖组件化开发、CSS定位和动态数据绑定。 组件结构设计 创建一个独立的Vue组件…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue实现标签

vue实现标签

Vue 实现标签的方法 在 Vue 中实现标签功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 动态组件可以根据不同的条件渲染不同的标签内容。通过 :is 属性动态绑定组件名,实现标签…

vue tab实现

vue tab实现

Vue Tab 实现方法 使用动态组件实现 Tab 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现 Ta…