当前位置:首页 > VUE

vue实现左右滑动组件

2026-02-22 21:11:34VUE

Vue 实现左右滑动组件

方案一:基于 touch 事件实现

监听 touchstarttouchmovetouchend 事件,计算滑动距离和方向:

<template>
  <div 
    class="slider"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    <div class="slider-content" :style="contentStyle">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      moveX: 0,
      currentTranslate: 0,
      isDragging: false
    }
  },
  computed: {
    contentStyle() {
      return {
        transform: `translateX(${this.currentTranslate}px)`,
        transition: this.isDragging ? 'none' : 'transform 0.3s ease'
      }
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX
      this.isDragging = true
    },
    handleTouchMove(e) {
      if (!this.isDragging) return
      this.moveX = e.touches[0].clientX - this.startX
      this.currentTranslate += this.moveX
      this.startX = e.touches[0].clientX
    },
    handleTouchEnd() {
      this.isDragging = false
      // 这里可以添加滑动结束后的逻辑,如自动对齐到最近的item
    }
  }
}
</script>

<style>
.slider {
  overflow: hidden;
  position: relative;
}
.slider-content {
  display: flex;
  will-change: transform;
}
</style>

方案二:使用第三方库 vue-awesome-swiper

安装 Swiper 库:

npm install swiper vue-awesome-swiper

组件实现:

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(item, index) in items" :key="index">
      <!-- 幻灯片内容 -->
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

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

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      items: [/* 你的数据 */],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination'
        },
        slidesPerView: 'auto',
        spaceBetween: 20,
        freeMode: true
      }
    }
  }
}
</script>

方案三:基于 CSS Scroll Snap

纯 CSS 方案,现代浏览器支持:

vue实现左右滑动组件

<template>
  <div class="scroll-container">
    <div class="scroll-content">
      <div v-for="(item, index) in items" :key="index" class="scroll-item">
        <!-- 项目内容 -->
      </div>
    </div>
  </div>
</template>

<style>
.scroll-container {
  width: 100%;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch; /* iOS平滑滚动 */
}

.scroll-content {
  display: flex;
  width: max-content;
}

.scroll-item {
  scroll-snap-align: start;
  flex: 0 0 auto;
  width: 80vw; /* 根据需要调整 */
  margin-right: 10px;
}
</style>

性能优化建议

  • 使用 will-change: transform 提升动画性能
  • 对于大量项目,考虑虚拟滚动技术
  • 移动端确保添加 -webkit-overflow-scrolling: touch 属性
  • 避免在滑动过程中频繁触发重绘操作

注意事项

  • 触摸事件方案需要考虑边界情况处理
  • 第三方库方案功能更全面但会增加包体积
  • CSS Scroll Snap 方案最简单但兼容性稍差
  • 根据项目需求选择合适的技术方案

标签: 组件vue
分享给朋友:

相关文章

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue实现抽奖

vue实现抽奖

Vue 实现抽奖功能 基本抽奖逻辑 创建一个Vue组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。 <template> <div> <…

vue拖动实现

vue拖动实现

实现 Vue 拖动功能的方法 使用 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的原生拖放 API 实现拖动功能。通过 draggable 属性标记可拖动元素,结合 @dragsta…

vue实现网站

vue实现网站

Vue 实现网站的基本步骤 Vue.js 是一个渐进式 JavaScript 框架,适合构建现代化的单页应用(SPA)或动态网站。以下是使用 Vue 实现网站的基本方法。 初始化 Vue 项目 使用…

vue实现发帖

vue实现发帖

使用 Vue 实现发帖功能 前端实现 创建发帖表单组件,包含标题和内容输入框,以及提交按钮。使用 v-model 绑定表单数据。 <template> <div>…

vue遮罩实现

vue遮罩实现

实现 Vue 遮罩的常见方法 使用 CSS 定位和透明度 创建一个全屏遮罩层,通过 CSS 固定定位覆盖整个视口,并设置半透明背景色。 <template> <div cl…