当前位置:首页 > VUE

vue实现左右滑动组件

2026-02-22 21:11:34VUE

Vue 实现左右滑动组件

方案一:基于 touch 事件实现

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

vue实现左右滑动组件

<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 库:

vue实现左右滑动组件

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 方案,现代浏览器支持:

<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 CLI或Vite创建项目,安装必要依赖如socket.io-client或axios(根据通信方式选择)。核心组件通常包括: ChatWindow.…

vue实现头像剪切

vue实现头像剪切

Vue 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm inst…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <ul…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…