当前位置:首页 > VUE

vue实现左右滑动效果

2026-02-23 18:14:42VUE

实现左右滑动效果的常见方法

使用Touch事件监听

通过监听touchstarttouchmovetouchend事件实现基础滑动逻辑:

<template>
  <div 
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
    class="slider-container">
    <!-- 滑动内容 -->
  </div>
</template>

<script>
export default {
  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
      // 实时更新元素位置
      e.currentTarget.style.transform = `translateX(${this.moveX}px)`
    },
    handleTouchEnd() {
      // 滑动结束后的处理逻辑
    }
  }
}
</script>

使用CSS Scroll Snap

结合CSS原生滚动特性实现流畅滑动:

vue实现左右滑动效果

.slider-container {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch; /*  iOS平滑滚动 */
}

.slide-item {
  scroll-snap-align: start;
  flex-shrink: 0;
  width: 100vw;
}

使用第三方库(Swiper.js)

安装Swiper库获得完整滑动组件:

npm install swiper

组件实现示例:

vue实现左右滑动效果

<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="(item, index) in slides" :key="index">
      {{ item }}
    </swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/swiper-bundle.css'

export default {
  components: { Swiper, SwiperSlide },
  data() {
    return {
      swiperOptions: {
        slidesPerView: 1,
        spaceBetween: 30,
        navigation: true
      },
      slides: ['Slide 1', 'Slide 2', 'Slide 3']
    }
  }
}
</script>

使用Vue专属库(vue-awesome-swiper)

专为Vue优化的滑动库实现:

<template>
  <swiper ref="mySwiper" :options="swiperOptions">
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image">
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

<script>
import 'swiper/css/swiper.css'
import { swiper, swiperSlide } from 'vue-awesome-swiper'

export default {
  components: {
    swiper,
    swiperSlide
  },
  data() {
    return {
      swiperOptions: {
        pagination: {
          el: '.swiper-pagination'
        },
        loop: true
      }
    }
  }
}
</script>

性能优化建议

  • 使用transform代替left/top定位实现硬件加速
  • 移动端添加-webkit-overflow-scrolling: touch增强滚动流畅度
  • 大量滑动项时采用虚拟滚动技术
  • 滑动动画使用requestAnimationFrame实现

手势判断逻辑示例

检测有效滑动方向的核心代码:

handleTouchEnd() {
  const threshold = 50 // 滑动生效阈值
  if (Math.abs(this.moveX) > threshold) {
    this.moveX > 0 
      ? this.goToPrevSlide() 
      : this.goToNextSlide()
  }
}

标签: 效果vue
分享给朋友:

相关文章

vue实现选择季度

vue实现选择季度

Vue 实现选择季度的几种方法 使用下拉选择框(Select) 在 Vue 中可以通过 v-model 绑定一个下拉选择框来实现季度选择。数据可以预先定义为一个包含季度选项的数组。 <…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…

vue实现列表

vue实现列表

Vue 实现列表的方法 在 Vue 中实现列表渲染通常使用 v-for 指令,可以动态生成多个元素。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 遍历数组,生成列表项。v-for 需要…