当前位置:首页 > VUE

vue实现水平滚动

2026-03-08 07:14:51VUE

Vue 实现水平滚动的方法

使用 CSS 的 overflow-x 属性

在 Vue 组件的模板中,创建一个容器元素,并为其添加 overflow-x: auto 样式。确保子元素宽度超过容器宽度。

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

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

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

.scroll-item {
  display: inline-block;
  width: 200px;
  height: 100px;
  margin-right: 10px;
  background: #eee;
}
</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' },
        { id: 3, text: 'Item 3' }
      ]
    }
  }
}
</script>

<style scoped>
.item {
  width: 200px;
  height: 100px;
  margin-right: 10px;
  background: #eee;
}
</style>

使用 CSS Flexbox 布局

通过 Flexbox 实现水平滚动,确保子元素不换行。

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

<style scoped>
.flex-scroll {
  display: flex;
  overflow-x: auto;
  width: 100%;
}

.flex-item {
  flex: 0 0 auto;
  width: 200px;
  height: 100px;
  margin-right: 10px;
  background: #eee;
}
</style>

使用 JavaScript 控制滚动

通过 Vue 的 ref 和 JavaScript 控制滚动行为。

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

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

<style scoped>
.js-scroll {
  display: flex;
  overflow-x: auto;
  width: 100%;
}

.js-item {
  flex: 0 0 auto;
  width: 200px;
  height: 100px;
  margin-right: 10px;
  background: #eee;
}
</style>

注意事项

  • 确保容器宽度有限,子元素宽度总和超过容器宽度才能触发滚动。
  • 使用 white-space: nowrapflex-wrap: nowrap 防止子元素换行。
  • 对于触控设备,可以添加 -webkit-overflow-scrolling: touch 以启用平滑滚动。

标签: 水平vue
分享给朋友:

相关文章

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(如…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…

vue nexttick 实现

vue nexttick 实现

Vue nextTick 实现原理 Vue 的 nextTick 是一个用于在下次 DOM 更新循环结束之后执行延迟回调的方法。它主要用于确保在 DOM 更新完成后执行某些操作。 核心实现机制 ne…