当前位置:首页 > VUE

vue实现横向滑动

2026-02-19 19:49:03VUE

Vue 实现横向滑动的方法

使用 CSS 和 Vue 结合实现横向滚动

通过 CSS 的 overflow-xwhite-space 属性,结合 Vue 的动态数据绑定,可以轻松实现横向滑动效果。

<template>
  <div class="horizontal-scroll-container">
    <div class="horizontal-scroll-content">
      <div v-for="item in items" :key="item.id" class="scroll-item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

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

<style>
.horizontal-scroll-container {
  width: 100%;
  overflow-x: auto;
  white-space: nowrap;
}

.horizontal-scroll-content {
  display: inline-block;
}

.scroll-item {
  display: inline-block;
  width: 200px;
  height: 100px;
  margin-right: 10px;
  background-color: #f0f0f0;
  text-align: center;
  line-height: 100px;
}
</style>

使用第三方库实现更复杂的横向滑动

对于更复杂的横向滑动需求,可以使用第三方库如 vue-horizontal-scrollswiper

安装 vue-horizontal-scroll

npm install vue-horizontal-scroll

示例代码:

<template>
  <vue-horizontal>
    <div v-for="item in items" :key="item.id" class="item">
      {{ item.text }}
    </div>
  </vue-horizontal>
</template>

<script>
import VueHorizontal from "vue-horizontal-scroll";

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

<style>
.item {
  width: 200px;
  height: 100px;
  margin-right: 10px;
  background-color: #f0f0f0;
  text-align: center;
  line-height: 100px;
}
</style>

使用 Swiper 实现横向滑动轮播

Swiper 是一个功能强大的滑动库,支持横向滑动、轮播、分页等功能。

vue实现横向滑动

安装 Swiper:

npm install swiper vue-awesome-swiper

示例代码:

<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="item in items" :key="item.id">
      {{ item.text }}
    </swiper-slide>
  </swiper>
</template>

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

export default {
  components: {
    Swiper,
    SwiperSlide,
  },
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' },
        { id: 4, text: 'Item 4' },
        { id: 5, text: 'Item 5' },
      ],
      swiperOptions: {
        slidesPerView: 'auto',
        spaceBetween: 10,
        freeMode: true,
      },
    };
  },
};
</script>

<style>
.swiper-slide {
  width: 200px;
  height: 100px;
  background-color: #f0f0f0;
  text-align: center;
  line-height: 100px;
}
</style>

响应式设计考虑

为了在不同设备上都能良好显示横向滑动内容,可以通过媒体查询调整样式。

vue实现横向滑动

@media (max-width: 768px) {
  .horizontal-scroll-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

性能优化

对于大量数据的横向滑动,可以使用虚拟滚动技术,如 vue-virtual-scroller,以减少 DOM 节点数量,提升性能。

安装 vue-virtual-scroller

npm install vue-virtual-scroller

示例代码:

<template>
  <RecycleScroller
    class="scroller"
    :items="items"
    :item-size="200"
    direction="horizontal"
    key-field="id"
  >
    <template v-slot="{ item }">
      <div class="item">
        {{ item.text }}
      </div>
    </template>
  </RecycleScroller>
</template>

<script>
import { RecycleScroller } from 'vue-virtual-scroller';
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css';

export default {
  components: { RecycleScroller },
  data() {
    return {
      items: Array.from({ length: 100 }, (_, i) => ({ id: i, text: `Item ${i}` })),
    };
  },
};
</script>

<style>
.scroller {
  width: 100%;
  height: 100px;
  overflow-x: auto;
}

.item {
  display: inline-block;
  width: 200px;
  height: 100px;
  margin-right: 10px;
  background-color: #f0f0f0;
  text-align: center;
  line-height: 100px;
}
</style>

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

相关文章

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template> &…

vue实现方法

vue实现方法

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

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <templat…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

vue 动画 实现

vue 动画 实现

vue 动画实现方法 Vue 提供了多种方式实现动画效果,主要包括过渡动画和动态动画两类。 使用 <transition> 组件实现基础过渡 通过 Vue 内置的 <transit…

vue导航实现

vue导航实现

Vue 导航实现方法 在 Vue 中实现导航功能通常涉及路由配置、组件设计和状态管理。以下是几种常见的实现方式: 使用 Vue Router 实现基础导航 安装 Vue Router: npm i…