当前位置:首页 > 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 实现无限纵向滚动。

vue实现纵向轮播

<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中可以通过集成拼音转换库(如pinyin或pinyin-pro)实现这一功能。 安装拼音转换库…

vue实现页面截屏

vue实现页面截屏

使用html2canvas库实现截屏 html2canvas是一个流行的JavaScript库,可以将DOM元素转换为Canvas图像。在Vue项目中安装html2canvas: npm insta…

vue实现一个tab栏

vue实现一个tab栏

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

vue 实现jqslidedown

vue 实现jqslidedown

在 Vue 中实现类似 jQuery 的 slideDown 效果,可以通过 Vue 的过渡系统或 CSS 动画结合动态样式绑定来实现。以下是几种实现方式: 使用 Vue Transition 组件…

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签数组…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…