当前位置:首页 > VUE

vue实现横向滚动

2026-01-18 18:02:18VUE

vue实现横向滚动

使用CSS的overflow-x属性结合Vue的模板和样式绑定,可以轻松实现横向滚动效果。

方法一:纯CSS实现

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

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

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

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

方法二:使用第三方库 安装vue-horizontal-scroll库:

npm install vue-horizontal-scroll

在组件中使用:

<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: [...]
    }
  }
}
</script>

方法三:自定义滚动行为 通过监听鼠标事件和触摸事件实现更复杂的交互:

<template>
  <div 
    class="scroll-container" 
    ref="scrollContainer"
    @mousedown="startDrag"
    @mousemove="onDrag"
    @mouseup="stopDrag"
    @mouseleave="stopDrag"
    @touchstart="startDrag"
    @touchmove="onDrag"
    @touchend="stopDrag"
  >
    <div class="scroll-content" :style="{ transform: `translateX(${scrollX}px)` }">
      <div v-for="item in items" :key="item.id" class="item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDragging: false,
      startX: 0,
      scrollX: 0,
      items: [...]
    }
  },
  methods: {
    startDrag(e) {
      this.isDragging = true
      this.startX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX
    },
    onDrag(e) {
      if (!this.isDragging) return
      const x = e.type.includes('touch') ? e.touches[0].clientX : e.clientX
      const walk = x - this.startX
      this.scrollX += walk
      this.startX = x
    },
    stopDrag() {
      this.isDragging = false
    }
  }
}
</script>

<style scoped>
.scroll-container {
  width: 100%;
  overflow: hidden;
  cursor: grab;
}

.scroll-content {
  display: flex;
  transition: transform 0.3s ease;
}

.scroll-container:active {
  cursor: grabbing;
}

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

注意事项

确保容器有明确的宽度限制,否则横向滚动可能不会按预期工作。

对于移动端设备,需要添加-webkit-overflow-scrolling: touch以启用平滑滚动:

.horizontal-scroll-container {
  -webkit-overflow-scrolling: touch;
}

考虑添加滚动条样式美化或隐藏,根据项目需求调整:

.horizontal-scroll-container::-webkit-scrollbar {
  height: 4px;
}

.horizontal-scroll-container::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 2px;
}

vue实现横向滚动

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

相关文章

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-s…

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListen…

vue computed实现

vue computed实现

计算属性的基本用法 在Vue中,计算属性通过computed选项定义,用于声明依赖于其他属性的动态值。计算属性基于它们的依赖进行缓存,只有依赖发生变化时才会重新计算。 new Vue({ da…

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复用…