当前位置:首页 > VUE

vue实现竖向轮播

2026-02-20 07:40:37VUE

Vue 实现竖向轮播的方法

竖向轮播可以通过多种方式实现,以下是一种基于 Vue 和 CSS 的常见实现方法。

使用 CSS 动画和 Vue 动态绑定

通过 Vue 的动态绑定和 CSS 的 transform 属性,可以实现平滑的竖向轮播效果。

vue实现竖向轮播

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

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'],
      currentIndex: 0,
      itemHeight: 100, // 每个轮播项的高度
      currentTranslateY: 0,
      interval: null
    };
  },
  mounted() {
    this.startAutoPlay();
  },
  beforeDestroy() {
    clearInterval(this.interval);
  },
  methods: {
    startAutoPlay() {
      this.interval = setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.items.length;
        this.currentTranslateY = -this.currentIndex * this.itemHeight;
      }, 2000);
    }
  }
};
</script>

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

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

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

使用第三方库(如 Swiper)

如果需要更复杂的功能(如触摸滑动、无限循环等),可以使用 Swiper 库。

安装 Swiper:

vue实现竖向轮播

npm install swiper

在 Vue 中使用 Swiper:

<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(item, index) in items" :key="index">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
import Swiper from 'swiper';
import 'swiper/css/swiper.css';

export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']
    };
  },
  mounted() {
    new Swiper('.swiper-container', {
      direction: 'vertical',
      loop: true,
      autoplay: {
        delay: 2000,
      },
    });
  }
};
</script>

<style>
.swiper-container {
  height: 100px;
}

.swiper-slide {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f0f0f0;
}
</style>

使用 Vue 过渡动画

Vue 的 <transition-group> 可以结合 CSS 过渡实现竖向轮播效果。

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

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'],
      visibleItems: ['Item 1', 'Item 2', 'Item 3'],
      interval: null
    };
  },
  mounted() {
    this.startAutoPlay();
  },
  beforeDestroy() {
    clearInterval(this.interval);
  },
  methods: {
    startAutoPlay() {
      this.interval = setInterval(() => {
        this.items.push(this.items.shift());
        this.visibleItems = this.items.slice(0, 3);
      }, 2000);
    }
  }
};
</script>

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

.carousel-container {
  height: 100%;
}

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

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

.slide-enter {
  transform: translateY(-100px);
  opacity: 0;
}

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

以上方法可以根据需求选择适合的实现方式。

标签: vue
分享给朋友:

相关文章

vue实现一个tab栏

vue实现一个tab栏

Vue 实现 Tab 栏 基础实现(选项式 API) <template> <div class="tab-container"> <div class="t…

vue实现语音

vue实现语音

Vue 实现语音功能的方法 使用 Web Speech API Vue 可以结合 Web Speech API 实现语音识别和合成功能。Web Speech API 包含 SpeechRecognit…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

vue diff实现

vue diff实现

Vue Diff 算法实现原理 Vue 的虚拟 DOM Diff 算法(即 patch 过程)是其高效渲染的核心,主要通过对比新旧虚拟节点的差异,最小化 DOM 操作。以下是关键实现要点: 同层级比…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…