当前位置:首页 > VUE

vue实现列表横向滑动

2026-01-22 12:24:10VUE

Vue 实现列表横向滑动的方法

使用 CSS Flexbox 和 overflow-x

通过 CSS Flexbox 布局结合 overflow-x: auto 可以实现横向滚动的列表。

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

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' },
        // 更多项目...
      ]
    }
  }
}
</script>

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

.scroll-list {
  display: flex;
  flex-wrap: nowrap;
  gap: 10px;
  padding: 10px 0;
}

.scroll-item {
  flex: 0 0 auto;
  width: 100px;
  height: 100px;
  background: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>

使用第三方库(如 vue-horizontal)

如果需要更复杂的功能,可以考虑使用专门的横向滚动库。

安装 vue-horizontal:

npm install vue-horizontal

使用示例:

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

<script>
import VueHorizontal from "vue-horizontal";

export default {
  components: { VueHorizontal },
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        // 更多项目...
      ]
    }
  }
}
</script>

自定义触摸事件支持

如果需要支持触摸设备上的平滑滚动,可以添加 touch 事件处理。

<template>
  <div 
    class="scroll-container"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    <div class="scroll-list" ref="scrollList">
      <!-- 列表内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      touchStartX: 0,
      scrollLeft: 0
    }
  },
  methods: {
    handleTouchStart(e) {
      this.touchStartX = e.touches[0].pageX
      this.scrollLeft = this.$refs.scrollList.scrollLeft
    },
    handleTouchMove(e) {
      const x = e.touches[0].pageX
      const walk = (x - this.touchStartX) * 2
      this.$refs.scrollList.scrollLeft = this.scrollLeft - walk
    }
  }
}
</script>

注意事项

  • 确保容器有明确的宽度限制,否则滚动可能无法正常工作
  • 移动设备上可能需要添加 -webkit-overflow-scrolling: touch 来改善滚动体验
  • 对于大量数据,考虑使用虚拟滚动技术优化性能

以上方法可以根据具体需求选择使用,CSS Flexbox 方案适合简单场景,而第三方库则提供更多高级功能。

vue实现列表横向滑动

标签: 横向列表
分享给朋友:

相关文章

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue 实现长列表

vue 实现长列表

vue 实现长列表的优化方法 使用虚拟滚动技术,只渲染可视区域内的元素,大幅减少DOM节点数量。通过计算滚动位置动态更新显示内容,降低内存占用和渲染压力。 <template> &l…

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaS…

vue列表查询实现

vue列表查询实现

实现Vue列表查询功能 基本数据绑定与渲染 在Vue中实现列表查询,首先需要定义数据源和查询条件。通过v-model绑定搜索输入框,使用计算属性过滤列表。 <template> &l…

vue实现列表添加

vue实现列表添加

使用 Vue 实现列表添加功能 数据绑定与列表渲染 在 Vue 中,通过 v-for 指令可以轻松实现列表渲染。首先需要定义一个数组来存储列表数据,并在模板中使用 v-for 循环渲染。 <…

vue实现列表循环

vue实现列表循环

列表循环的实现方式 在Vue中实现列表循环主要依赖于v-for指令,该指令基于源数据多次渲染元素或模板块。 基础数组循环 使用v-for遍历数组时,语法为item in items或(item,…