当前位置:首页 > 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 库:

npm install vue-horizontal-scroll

在 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 控制滚动行为。

vue实现水平滚动

<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实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue 路由 实现

vue 路由 实现

Vue 路由实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue 路由的详细步骤: 安装 Vue Router 通过 npm 或 ya…

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

jquery实现vue

jquery实现vue

jQuery 和 Vue.js 是两种不同理念的前端工具,jQuery 以 DOM 操作和事件驱动为核心,而 Vue.js 是数据驱动的现代化框架。如果想用 jQuery 实现类似 Vue 的功能(如…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…