当前位置:首页 > 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实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了原…

vue实现框选

vue实现框选

Vue 实现框选功能 在 Vue 中实现框选功能通常需要监听鼠标事件,计算选区范围,并根据选区范围高亮或选中元素。以下是实现框选功能的关键步骤。 监听鼠标事件 在 Vue 模板中,为容器元素绑定鼠标…

vue指令实现

vue指令实现

Vue 指令的实现方式 Vue 指令是特殊的标记,用于在 DOM 元素上应用特定的行为。可以通过全局或局部方式注册自定义指令。 全局注册指令 在 Vue 应用中通过 Vue.directive 方法…

vue实现全选下载

vue实现全选下载

Vue实现全选下载功能 全选下载功能通常涉及前端文件列表的多选操作和批量下载逻辑。以下是基于Vue的实现方案: 数据准备与渲染 在Vue组件中定义文件列表数据和选中状态: data() { r…

vue怎么实现滑动

vue怎么实现滑动

Vue实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的过渡系统和CSS可以实现平滑的滑动效果。定义一个CSS类,利用transf…