当前位置:首页 > 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中为列表添加水印可以通过多种方式实现,以下是几种常见的方法: 使用CSS背景图 通过CSS的background-image属性为列表元素添加水印背景。水印可以是文字或图…

vue 实现长列表

vue 实现长列表

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

vue实现横向导航

vue实现横向导航

Vue 实现横向导航的方法 使用 Flex 布局实现横向导航 在 Vue 中,可以通过 CSS Flex 布局快速实现横向导航。创建一个导航组件,并使用 Flex 布局使其水平排列。 <te…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 无限滚动列表通常用于展示大量数据,通过动态加载数据减少初始渲染压力。以下是基于Vue的实现方法: 使用Intersection Observer API监听滚动 Inter…

vue实现列表显示

vue实现列表显示

使用 Vue 实现列表显示 基础列表渲染 在 Vue 中,可以通过 v-for 指令实现列表渲染。以下是一个简单的示例: <template> <ul> <…

vue公共列表的实现

vue公共列表的实现

Vue 公共列表的实现方法 在 Vue 中实现公共列表组件,可以通过封装可复用的逻辑和模板来完成。以下是几种常见的实现方式: 使用 props 和 slots 通过 props 接收列表数据,使用…