当前位置:首页 > 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实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { -…

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue实现vnc

vue实现vnc

Vue 实现 VNC 客户端 在 Vue 项目中实现 VNC 客户端功能,可以通过集成现有的 VNC 客户端库或组件完成。以下是具体实现方法: 安装依赖库 使用 noVNC 或其他 VNC 客户端…