当前位置:首页 > VUE

vue实现触摸轮播

2026-02-17 17:45:40VUE

Vue 实现触摸轮播

使用第三方库(如 Swiper)

安装 Swiper 库:

npm install swiper

在 Vue 组件中引入 Swiper:

<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(item, index) in slides" :key="index">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
import Swiper from 'swiper';
import 'swiper/css/swiper.css';

export default {
  data() {
    return {
      slides: ['Slide 1', 'Slide 2', 'Slide 3']
    };
  },
  mounted() {
    new Swiper('.swiper-container', {
      loop: true,
      pagination: {
        el: '.swiper-pagination',
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
      touchEventsTarget: 'container',
      touchRatio: 1,
      touchAngle: 45,
      simulateTouch: true
    });
  }
};
</script>

自定义触摸事件实现

通过 Vue 的 @touchstart@touchmove@touchend 事件实现基础触摸轮播:

<template>
  <div 
    class="carousel"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    <div 
      class="slide-container"
      :style="{ transform: `translateX(${offsetX}px)` }"
    >
      <div v-for="(item, index) in slides" :key="index" class="slide">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      slides: ['Slide 1', 'Slide 2', 'Slide 3'],
      currentIndex: 0,
      offsetX: 0,
      startX: 0,
      moveX: 0
    };
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX;
    },
    handleTouchMove(e) {
      this.moveX = e.touches[0].clientX - this.startX;
      this.offsetX = this.moveX;
    },
    handleTouchEnd() {
      const threshold = 50;
      if (Math.abs(this.moveX) > threshold) {
        if (this.moveX > 0) {
          this.currentIndex = Math.max(0, this.currentIndex - 1);
        } else {
          this.currentIndex = Math.min(this.slides.length - 1, this.currentIndex + 1);
        }
      }
      this.offsetX = -this.currentIndex * 100;
      this.moveX = 0;
    }
  }
};
</script>

<style>
.carousel {
  overflow: hidden;
  position: relative;
  width: 100%;
}
.slide-container {
  display: flex;
  transition: transform 0.3s ease;
}
.slide {
  flex: 0 0 100%;
  width: 100%;
}
</style>

优化触摸体验

添加过渡动画和边界检测:

methods: {
  handleTouchEnd() {
    const threshold = 50;
    if (Math.abs(this.moveX) > threshold) {
      if (this.moveX > 0 && this.currentIndex > 0) {
        this.currentIndex--;
      } else if (this.moveX < 0 && this.currentIndex < this.slides.length - 1) {
        this.currentIndex++;
      }
    }
    this.animateSlide();
  },
  animateSlide() {
    const targetX = -this.currentIndex * 100;
    const distance = targetX - this.offsetX;
    const duration = 300;
    const startTime = performance.now();

    const animate = (currentTime) => {
      const elapsed = currentTime - startTime;
      const progress = Math.min(elapsed / duration, 1);
      this.offsetX = this.offsetX + distance * progress;
      if (progress < 1) {
        requestAnimationFrame(animate);
      }
    };
    requestAnimationFrame(animate);
  }
}

注意事项

  • 移动端开发需添加 meta 标签禁用默认手势:
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  • 对于更复杂的需求(如无限循环、垂直滑动),推荐直接使用 Swiper 等成熟库。
  • 自定义实现时注意处理快速滑动和边缘情况。

vue实现触摸轮播

标签: vue
分享给朋友:

相关文章

vue实现多人视频

vue实现多人视频

实现多人视频通话的Vue方案 使用WebRTC技术结合Vue框架可以构建多人视频通话应用。以下是核心实现方法: 技术栈选择 Vue 2/3作为前端框架 WebRTC用于实时通信 Socket.io…

课程表vue实现

课程表vue实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ {…

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽 子…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式…