当前位置:首页 > VUE

vue实现滑动翻页

2026-03-28 19:02:56VUE

Vue 实现滑动翻页方案

监听触摸事件
通过 @touchstart@touchmove@touchend 监听手势,记录滑动起始位置和偏移量:

data() {
  return {
    startX: 0,
    moveX: 0
  }
},
methods: {
  handleTouchStart(e) {
    this.startX = e.touches[0].clientX
  },
  handleTouchMove(e) {
    this.moveX = e.touches[0].clientX - this.startX
  },
  handleTouchEnd() {
    if (Math.abs(this.moveX) > 50) { // 滑动阈值
      this.moveX > 0 ? this.prevPage() : this.nextPage()
    }
    this.moveX = 0
  }
}

绑定事件到容器
在模板中为滑动区域添加事件绑定:

<div 
  @touchstart="handleTouchStart"
  @touchmove="handleTouchMove"
  @touchend="handleTouchEnd"
  class="swipe-container"
>
  <!-- 翻页内容 -->
</div>

添加过渡动画
使用 Vue 的 <transition> 组件实现平滑翻页效果:

<transition :name="transitionName">
  <div :key="currentPage">
    <!-- 当前页内容 -->
  </div>
</transition>
.slide-left-enter-active,
.slide-left-leave-active {
  transition: transform 0.3s ease;
}
.slide-left-enter {
  transform: translateX(100%);
}
.slide-left-leave-to {
  transform: translateX(-100%);
}
/* 反向滑动样式同理 */

分页控制逻辑
维护当前页码并限制边界:

data() {
  return {
    currentPage: 1,
    totalPages: 5
  }
},
methods: {
  nextPage() {
    if (this.currentPage < this.totalPages) {
      this.transitionName = 'slide-left'
      this.currentPage++
    }
  },
  prevPage() {
    if (this.currentPage > 1) {
      this.transitionName = 'slide-right'
      this.currentPage--
    }
  }
}

优化建议

vue实现滑动翻页

  • 添加 touch-action: pan-y CSS 属性避免浏览器默认行为冲突
  • 使用 requestAnimationFrame 优化滑动性能
  • 移动端适配需考虑 passive: true 事件选项提升滚动性能

标签: 翻页vue
分享给朋友:

相关文章

vue设计与实现 书

vue设计与实现 书

vue设计与实现 书 《Vue.js设计与实现》是一本深入探讨Vue.js框架内部原理和设计思想的书籍,适合希望深入理解Vue.js的开发者。以下是关于这本书的详细信息: 书籍内容 《Vue…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue toast实现

vue toast实现

Vue Toast 实现方法 使用第三方库(推荐) 安装 vue-toastification 库,这是一个功能丰富且轻量级的 Vue Toast 插件。 npm install vue-toast…

vue实现循环

vue实现循环

Vue 实现循环的方法 在 Vue 中,可以通过 v-for 指令实现循环渲染列表或对象。以下是几种常见的用法: 遍历数组 使用 v-for 遍历数组时,可以同时获取当前项和索引: <ul&…

vue实现tip

vue实现tip

Vue实现Tooltip的方法 使用Vue实现Tooltip可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML的title属性 在Vue模板中直接使用HTML的title属性是最简单的实…