当前位置:首页 > 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方法中可以添加节流逻辑,限制事件触发频率。

vue 左右滑动实现

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

标签: vue
分享给朋友:

相关文章

vue实现引导

vue实现引导

Vue 实现引导功能的方法 使用第三方库 推荐使用 driver.js 或 intro.js 这类专门为 Vue 设计的引导库。这些库提供了完整的 API 和样式,能够快速实现引导功能。 安装 dr…

vue实现markdown

vue实现markdown

Vue 实现 Markdown 编辑器 在 Vue 中实现 Markdown 编辑器可以通过集成第三方库或自定义解析器完成。以下是两种常见方法: 使用第三方库(如 marked 或 markdown…

vue原理实现

vue原理实现

Vue 原理实现的核心机制 Vue.js 的核心原理基于响应式系统、虚拟 DOM 和模板编译。以下是其核心实现机制的分解: 响应式系统 Vue 使用 Object.defineProperty(Vu…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue实现pwa

vue实现pwa

Vue 实现 PWA 的步骤 Vue 结合 PWA(Progressive Web App)可以创建离线可用的 Web 应用。以下是实现方法: 安装 Vue CLI 并创建项目 确保已安装 Vue…

vue实现hovuer

vue实现hovuer

Vue 实现 Hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 直接控制 通过 :hover 伪类实现,无需额外的 JavaScrip…