当前位置:首页 > VUE

vue 左右滑动实现

2026-02-10 23:43:41VUE

实现左右滑动的基本思路

在Vue中实现左右滑动效果,可以通过监听触摸事件(touchstart、touchmove、touchend)来计算滑动距离和方向。结合CSS的transform属性实现平滑的滑动动画效果。

使用原生事件监听实现

创建一个Vue组件,通过监听触摸事件来实现左右滑动:

<template>
  <div 
    class="slider"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    <div 
      class="slider-content"
      :style="{ transform: `translateX(${translateX}px)` }"
    >
      <!-- 滑动内容 -->
      <div v-for="item in items" :key="item.id" class="slide">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, content: 'Slide 1' },
        { id: 2, content: 'Slide 2' },
        { id: 3, content: 'Slide 3' }
      ],
      startX: 0,
      translateX: 0,
      currentIndex: 0
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX
    },
    handleTouchMove(e) {
      const currentX = e.touches[0].clientX
      const diff = currentX - this.startX
      this.translateX = diff
    },
    handleTouchEnd(e) {
      const endX = e.changedTouches[0].clientX
      const diff = endX - this.startX

      // 滑动阈值,大于50px才切换
      if (Math.abs(diff) > 50) {
        if (diff > 0) {
          // 向右滑动
          this.currentIndex = Math.max(0, this.currentIndex - 1)
        } else {
          // 向左滑动
          this.currentIndex = Math.min(this.items.length - 1, this.currentIndex + 1)
        }
      }

      // 根据currentIndex计算最终位置
      this.translateX = -this.currentIndex * this.$el.offsetWidth
    }
  }
}
</script>

<style>
.slider {
  width: 100%;
  overflow: hidden;
  position: relative;
}
.slider-content {
  display: flex;
  transition: transform 0.3s ease;
}
.slide {
  flex: 0 0 100%;
  width: 100%;
}
</style>

使用第三方库实现

对于更复杂的需求,可以使用现成的Vue滑动组件库,如vue-awesome-swiper:

安装依赖:

npm install swiper vue-awesome-swiper

使用示例:

<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="item in items" :key="item.id">
      {{ item.content }}
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      items: [
        { id: 1, content: 'Slide 1' },
        { id: 2, content: 'Slide 2' },
        { id: 3, content: 'Slide 3' }
      ],
      swiperOptions: {
        pagination: {
          el: '.swiper-pagination'
        },
        // 可配置更多选项
        loop: true,
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      }
    }
  }
}
</script>

性能优化建议

对于大量滑动项的情况,考虑实现虚拟滚动或懒加载。监听滑动位置,动态加载可见区域附近的内容,减少DOM节点数量。

添加防抖处理避免频繁触发滑动事件。在handleTouchMove方法中可以添加节流逻辑,限制事件触发频率。

handleTouchMove: _.throttle(function(e) {
  const currentX = e.touches[0].clientX
  const diff = currentX - this.startX
  this.translateX = diff
}, 16) // 约60fps

vue 左右滑动实现

标签: vue
分享给朋友:

相关文章

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: da…

vue 路由 实现

vue 路由 实现

Vue 路由实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue 路由的详细步骤: 安装 Vue Router 通过 npm 或 ya…

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.de…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <ul…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…