当前位置:首页 > VUE

怎么实现vue横向滚动

2026-02-22 08:33:20VUE

实现 Vue 横向滚动的方法

使用 CSS 的 overflow-x 属性

在 Vue 组件中,通过 CSS 设置 overflow-x: autooverflow-x: scroll 实现横向滚动。
示例代码:

<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>

<style scoped>
.horizontal-scroll-container {
  overflow-x: auto;
  white-space: nowrap;
}
.horizontal-scroll-content {
  display: inline-block;
}
.scroll-item {
  display: inline-block;
  width: 200px;
  margin-right: 10px;
}
</style>

使用第三方库(如 vue-horizontal-scroll

安装 vue-horizontal-scroll 库:

怎么实现vue横向滚动

npm install vue-horizontal-scroll

在 Vue 组件中使用:

怎么实现vue横向滚动

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

<script>
import VueHorizontalScroll from 'vue-horizontal-scroll';

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

使用 Flexbox 布局

通过 Flexbox 的 flex-wrap: nowrap 实现横向滚动。
示例代码:

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

<style scoped>
.flex-scroll-container {
  display: flex;
  overflow-x: auto;
  flex-wrap: nowrap;
}
.flex-item {
  flex: 0 0 auto;
  width: 200px;
  margin-right: 10px;
}
</style>

使用 JavaScript 控制滚动

通过 ref 和 JavaScript 方法实现自定义滚动逻辑。
示例代码:

<template>
  <div ref="scrollContainer" class="scroll-container">
    <div class="scroll-content">
      <div v-for="item in items" :key="item.id" class="scroll-item">
        {{ item.text }}
      </div>
    </div>
    <button @click="scrollLeft">Left</button>
    <button @click="scrollRight">Right</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
      ],
    };
  },
  methods: {
    scrollLeft() {
      this.$refs.scrollContainer.scrollBy({ left: -100, behavior: 'smooth' });
    },
    scrollRight() {
      this.$refs.scrollContainer.scrollBy({ left: 100, behavior: 'smooth' });
    },
  },
};
</script>

<style scoped>
.scroll-container {
  overflow-x: auto;
  white-space: nowrap;
}
.scroll-content {
  display: inline-block;
}
.scroll-item {
  display: inline-block;
  width: 200px;
  margin-right: 10px;
}
</style>

注意事项

  • 确保内容宽度超过容器宽度,否则不会触发滚动。
  • 使用 white-space: nowrapflex-wrap: nowrap 防止内容换行。
  • 对于移动端,可以添加 -webkit-overflow-scrolling: touch 提升滚动体验。

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

相关文章

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或移…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…

vue实现筛选

vue实现筛选

实现筛选功能的基本思路 在Vue中实现筛选功能通常需要结合数据绑定、计算属性和方法。筛选的核心逻辑是根据用户输入的条件过滤原始数据列表,并动态更新显示结果。 数据准备 定义一个数组存储原始数据,另一…

vue实现mvvm

vue实现mvvm

Vue 实现 MVVM 模式 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 是其典型实现之一。MVVM 的核心是通过数据绑定和响应式系统实现视图与数据的自动同步。…