当前位置:首页 > 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实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交…

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在V…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…