当前位置:首页 > 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实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <template>…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 doc…

vue实现海报

vue实现海报

Vue 实现海报生成方案 使用 html2canvas 库 html2canvas 是一个将 HTML 元素转换为 Canvas 的库,适合将 Vue 组件渲染为海报图片 安装依赖: npm in…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…