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

vue实现横向滚动

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>

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

vue实现横向滚动

<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 Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue实现分屏

vue实现分屏

Vue 实现分屏的方法 分屏功能通常用于展示多个独立内容区域,以下是几种常见的实现方式: 使用 CSS Grid 布局 通过 CSS Grid 可以轻松创建分屏布局,在 Vue 组件中直接应用样式:…

vue实现遮罩

vue实现遮罩

Vue 实现遮罩层的方法 在 Vue 中实现遮罩层可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-show 或 v-if 控制显示 通过 Vue 的指令动态控制遮罩层的显示与隐藏。…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现异步

vue实现异步

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

vue实现分发

vue实现分发

Vue 实现分发的方法 在 Vue 中,分发(Distribution)通常指的是将内容从父组件传递到子组件,或者通过插槽(Slots)机制让父组件控制子组件的部分渲染内容。以下是几种常见的实现分发的…