当前位置:首页 > 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 特性实现更流畅的滑动体验:

vue实现左右滑动

.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 属性提升动画性能:

vue实现左右滑动

.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)
}

边界处理

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

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

标签: vue
分享给朋友:

相关文章

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件可…

vue 页签实现

vue 页签实现

Vue 页签实现方法 在 Vue 中实现页签功能通常涉及动态组件、路由或状态管理。以下是几种常见的实现方式: 使用动态组件 通过 Vue 的 <component> 动态组件结合 v-f…

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return { f…

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { -…

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPag…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…