当前位置:首页 > 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实现导航栏切换的方法 使用v-for和v-bind动态渲染导航项 通过v-for循环遍历导航数据数组,结合v-bind动态绑定class或style。当点击导航项时,更新当前选中项的索引或ID。…

vue实现检测数组

vue实现检测数组

Vue 中检测数组变化的方法 Vue 的响应式系统无法直接检测到数组的某些变化,因为 JavaScript 的限制。以下是一些解决方案: 使用 Vue.set 或 this.$set Vue…

vue的动画实现

vue的动画实现

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

vue实现好友列表

vue实现好友列表

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

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <d…

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&g…