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

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

vue实现横向滚动

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

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

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

相关文章

vue实现遮罩

vue实现遮罩

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

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导航…

vue实现点击跳转

vue实现点击跳转

Vue 实现点击跳转的方法 在 Vue 中实现点击跳转可以通过以下几种方式: 使用 router-link router-link 是 Vue Router 提供的组件,用于声明式导航。适合在模板中…

vue实现滑动条

vue实现滑动条

Vue 实现滑动条 在 Vue 中实现滑动条可以通过原生 HTML 的 <input type="range"> 或自定义组件实现。以下是两种常见方法: 使用原生 HTML 滑动条 通过…

laravel vue实现登录

laravel vue实现登录

Laravel 和 Vue.js 实现登录功能 后端配置(Laravel) 安装 Laravel Sanctum 用于 API 认证 composer require laravel/sanctu…

vue业务实现

vue业务实现

Vue 业务实现方法 组件化开发 Vue 的核心思想是组件化,将页面拆分为独立可复用的组件。每个组件包含模板、脚本和样式,通过 props 和 events 实现父子通信。大型项目可使用 Vuex 或…