当前位置:首页 > VUE

vue实现左右滑动列表

2026-02-21 10:25:30VUE

Vue 实现左右滑动列表的方法

使用 CSS 和 touch 事件

通过 CSS 的 overflow-xwhite-space 属性实现横向滚动,结合 Vue 的 @touchstart@touchmove@touchend 事件处理滑动逻辑。

<template>
  <div 
    class="scroll-container"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    <div class="scroll-content" :style="{ transform: `translateX(${offsetX}px)` }">
      <div v-for="item in items" :key="item.id" class="item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [...], // 列表数据
      startX: 0,
      offsetX: 0,
      isDragging: false
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX
      this.isDragging = true
    },
    handleTouchMove(e) {
      if (!this.isDragging) return
      const currentX = e.touches[0].clientX
      this.offsetX += currentX - this.startX
      this.startX = currentX
    },
    handleTouchEnd() {
      this.isDragging = false
      // 可以添加边界检测和惯性滑动逻辑
    }
  }
}
</script>

<style>
.scroll-container {
  width: 100%;
  overflow-x: hidden;
  touch-action: pan-y;
}
.scroll-content {
  display: flex;
  white-space: nowrap;
  transition: transform 0.3s ease;
}
.item {
  flex: 0 0 auto;
  width: 100px;
  height: 100px;
  margin-right: 10px;
  background: #eee;
}
</style>

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

安装 vue-swipe 库可以快速实现滑动列表功能。

npm install vue-swipe --save
<template>
  <swipe class="my-swipe">
    <swipe-item v-for="item in items" :key="item.id">
      {{ item.text }}
    </swipe-item>
  </swipe>
</template>

<script>
import { Swipe, SwipeItem } from 'vue-swipe'

export default {
  components: {
    Swipe,
    SwipeItem
  },
  data() {
    return {
      items: [...] // 列表数据
    }
  }
}
</script>

<style>
.my-swipe {
  height: 100px;
}
</style>

使用 CSS Scroll Snap

CSS Scroll Snap 提供原生滚动对齐功能,适合实现分页滑动效果。

vue实现左右滑动列表

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

<style>
.scroll-container {
  width: 100%;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}
.scroll-content {
  display: flex;
  width: max-content;
}
.item {
  scroll-snap-align: start;
  width: 100vw;
  height: 100px;
  flex: 0 0 auto;
}
</style>

以上方法可根据项目需求选择,CSS 方案适合简单需求,第三方库提供更丰富的功能,CSS Scroll Snap 则利用浏览器原生特性实现流畅效果。

标签: 列表vue
分享给朋友:

相关文章

vue实现拍照

vue实现拍照

Vue实现拍照功能 在Vue中实现拍照功能通常需要结合浏览器的MediaDevices API和Canvas元素。以下是具体实现方法: 安装依赖(可选) 如果需要更简单的封装,可以安装vue-web…

vue 菜单实现

vue 菜单实现

Vue 菜单实现方法 在Vue中实现菜单功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-for动态生成菜单 通过数据驱动的方式动态渲染菜单项,适合菜单内容可能变化的场景: <t…

vue scrolltop 实现

vue scrolltop 实现

实现 Vue 中的 scrollTop 在 Vue 中实现滚动到顶部功能可以通过多种方式完成,包括使用原生 JavaScript、Vue 指令或第三方库。以下是几种常见的实现方法: 使用原生 Jav…

vue实现uuid

vue实现uuid

使用 uuid 库生成 UUID 在 Vue 项目中,可以使用 uuid 库来生成 UUID。安装依赖: npm install uuid 在 Vue 组件中使用: import { v4 as…

proxy实现vue

proxy实现vue

使用 Proxy 实现 Vue 响应式系统 Vue 3 的响应式系统基于 JavaScript 的 Proxy API,通过代理对象实现对属性的拦截和依赖追踪。以下是实现的核心逻辑: 创建响应式对象…

vue实现fragment

vue实现fragment

Vue 实现 Fragment 的方法 在 Vue 中,Fragment 允许组件返回多个根节点而不需要包裹一个额外的 DOM 元素。以下是几种实现方式: 使用 Vue 3 的 <templa…