当前位置:首页 > VUE

vue实现左右滑动

2026-01-08 15:49:35VUE

Vue 实现左右滑动效果

使用 touch 事件实现基础滑动

监听 touchstarttouchmovetouchend 事件实现基础滑动逻辑:

<template>
  <div 
    class="slider"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    <div class="slider-content" :style="contentStyle">
      <!-- 滑动内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      moveX: 0,
      offsetX: 0,
      isDragging: false
    }
  },
  computed: {
    contentStyle() {
      return {
        transform: `translateX(${this.offsetX}px)`,
        transition: this.isDragging ? 'none' : 'transform 0.3s ease'
      }
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX
      this.isDragging = true
    },
    handleTouchMove(e) {
      this.moveX = e.touches[0].clientX
      this.offsetX = this.moveX - this.startX
    },
    handleTouchEnd() {
      this.isDragging = false
      // 添加边界判断和回弹逻辑
    }
  }
}
</script>

使用 CSS Scroll Snap

结合 CSS 的 scroll-snap 特性实现更流畅的滑动体验:

.slider-container {
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}

.slide-item {
  scroll-snap-align: start;
  flex: 0 0 auto;
}

使用第三方库

对于更复杂的需求,可以使用专门的滑动库:

  1. Swiper Vue
    npm install swiper@latest
<template>
  <swiper
    :slides-per-view="1"
    :space-between="50"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
  </swiper>
</template>

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

export default {
  components: {
    Swiper,
    SwiperSlide
  }
}
</script>
  1. Vue-Awesome-Swiper
    npm install vue-awesome-swiper@4.1.1

性能优化技巧

添加 will-change 属性提升动画性能:

.slider-content {
  will-change: transform;
}

使用 requestAnimationFrame 优化 touchmove 事件:

let animationId = null

handleTouchMove(e) {
  const update = () => {
    this.moveX = e.touches[0].clientX
    this.offsetX = this.moveX - this.startX
    animationId = requestAnimationFrame(update)
  }
  cancelAnimationFrame(animationId)
  animationId = requestAnimationFrame(update)
}

边界处理

添加滑动边界判断防止过度滑动:

vue实现左右滑动

handleTouchEnd() {
  const maxOffset = window.innerWidth * 0.3
  if (Math.abs(this.offsetX) > maxOffset) {
    // 执行翻页逻辑
  } else {
    // 回弹到原位置
    this.offsetX = 0
  }
}

标签: vue
分享给朋友:

相关文章

vue实现贴图

vue实现贴图

Vue 实现贴图功能 在Vue中实现贴图功能通常涉及将图片动态添加到指定位置或元素上。以下是几种常见实现方式: 使用v-bind动态绑定图片 通过数据驱动的方式动态更换图片源: <templ…

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…

vue 实现协同

vue 实现协同

Vue 实现协同编辑的方案 协同编辑指多个用户同时编辑同一文档并实时同步更改。Vue 结合相关库和技术可轻松实现该功能。 使用 WebSocket 实现实时通信 WebSocket 提供全双工通信,…