当前位置:首页 > VUE

vue实现纵向轮播

2026-01-15 06:21:52VUE

Vue 实现纵向轮播的方法

纵向轮播可以通过 Vue 的动态组件和 CSS 动画实现。以下是几种常见的实现方式:

使用 CSS 动画和 Vue 过渡

通过 Vue 的 <transition> 组件结合 CSS 的 transformtransition 属性实现纵向滚动效果。

<template>
  <div class="carousel-container">
    <transition name="slide" mode="out-in">
      <div :key="currentIndex" class="carousel-item">
        {{ items[currentIndex] }}
      </div>
    </transition>
  </div>
</template>

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

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

.carousel-item {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

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

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

使用第三方库(如 Swiper)

Swiper 是一个功能强大的轮播库,支持纵向滚动。

<template>
  <swiper :direction="'vertical'" :autoplay="{ delay: 3000 }">
    <swiper-slide v-for="(item, index) in items" :key="index">
      {{ item }}
    </swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/swiper-bundle.css';

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4']
    };
  }
};
</script>

使用纯 CSS 动画

通过 CSS 的 @keyframes 实现无限纵向滚动。

<template>
  <div class="carousel">
    <div class="carousel-inner">
      <div v-for="(item, index) in items" :key="index" class="carousel-item">
        {{ item }}
      </div>
    </div>
  </div>
</template>

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

<style>
.carousel {
  height: 200px;
  overflow: hidden;
}

.carousel-inner {
  animation: scroll 10s linear infinite;
}

.carousel-item {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
}

@keyframes scroll {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-100%);
  }
}
</style>

注意事项

  • 如果需要手动控制轮播,可以添加按钮事件。
  • 轮播内容较多时,建议使用虚拟滚动优化性能。
  • 在移动端使用时,注意触摸事件的兼容性。

vue实现纵向轮播

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

相关文章

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转…

vue实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象,…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <…