当前位置:首页 > 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实现列表横向滑动

安装 vue-horizontal:

vue实现列表横向滑动

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的实现方法: 使用Intersection Observer API监听滚动 Inters…

h5 实现列表

h5 实现列表

列表实现方法 在HTML5中,可以通过多种方式实现列表,包括无序列表、有序列表和自定义列表。以下是具体的实现方法。 无序列表 无序列表使用<ul>标签,列表项使用<li>标签…

jquery列表

jquery列表

jQuery 列表操作 jQuery 提供了多种方法来操作 HTML 列表(如 <ul> 或 <ol>),包括动态添加、删除、修改列表项,以及事件绑定等。 创建列表 使用 j…

vue列表怎么实现

vue列表怎么实现

Vue 列表实现方法 使用 v-for 指令 Vue 中列表渲染主要通过 v-for 指令实现,可以遍历数组或对象。 <template> <ul> <li…

vue实现列表分类

vue实现列表分类

Vue 实现列表分类的方法 使用计算属性分类 通过计算属性对原始数组进行分类处理,返回分组后的对象或数组。适用于静态数据或需要响应式更新的场景。 computed: { categorizedI…

vue实现列表页面

vue实现列表页面

Vue 列表页面实现方案 基础列表渲染 使用 v-for 指令渲染数组数据,配合 :key 提升性能: <template> <ul> <li v-for=…