当前位置:首页 > VUE

vue实现滑动框

2026-01-16 07:15:23VUE

Vue 实现滑动框的方法

使用原生 CSS 和 Vue 事件

通过 Vue 的 v-on 指令绑定鼠标事件,结合 CSS 的 transform 实现滑动效果。

<template>
  <div class="slider-container">
    <div 
      class="slider"
      @mousedown="startDrag"
      @mousemove="onDrag"
      @mouseup="endDrag"
      @mouseleave="endDrag"
      :style="{ transform: `translateX(${position}px)` }"
    >
      滑动内容
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      position: 0,
      isDragging: false,
      startX: 0
    }
  },
  methods: {
    startDrag(e) {
      this.isDragging = true
      this.startX = e.clientX - this.position
    },
    onDrag(e) {
      if (!this.isDragging) return
      this.position = e.clientX - this.startX
    },
    endDrag() {
      this.isDragging = false
    }
  }
}
</script>

<style>
.slider-container {
  width: 100%;
  overflow: hidden;
  position: relative;
}
.slider {
  width: 200px;
  height: 100px;
  background: #eee;
  cursor: grab;
  user-select: none;
}
</style>

使用第三方库 vue-draggable

对于更复杂的拖拽需求,可以使用 vue-draggable 库。

安装依赖:

vue实现滑动框

npm install vuedraggable

示例代码:

<template>
  <draggable 
    v-model="list"
    class="drag-area"
    @start="dragStart"
    @end="dragEnd"
  >
    <div v-for="item in list" :key="item.id" class="drag-item">
      {{ item.name }}
    </div>
  </draggable>
</template>

<script>
import draggable from 'vuedraggable'

export default {
  components: { draggable },
  data() {
    return {
      list: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ]
    }
  },
  methods: {
    dragStart() {
      console.log('拖动开始')
    },
    dragEnd() {
      console.log('拖动结束')
    }
  }
}
</script>

<style>
.drag-area {
  width: 300px;
}
.drag-item {
  padding: 10px;
  margin: 5px;
  background: #f0f0f0;
  cursor: move;
}
</style>

实现触摸屏支持

对于移动端设备,需要添加触摸事件支持:

vue实现滑动框

<template>
  <div class="slider-container">
    <div 
      class="slider"
      @mousedown="startDrag"
      @touchstart="startDrag"
      @mousemove="onDrag"
      @touchmove="onDrag"
      @mouseup="endDrag"
      @touchend="endDrag"
      @mouseleave="endDrag"
      :style="{ transform: `translateX(${position}px)` }"
    >
      滑动内容
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    startDrag(e) {
      this.isDragging = true
      const clientX = e.clientX || e.touches[0].clientX
      this.startX = clientX - this.position
    },
    onDrag(e) {
      if (!this.isDragging) return
      const clientX = e.clientX || e.touches[0].clientX
      this.position = clientX - this.startX
      e.preventDefault()
    }
  }
}
</script>

添加边界限制

防止滑块滑出容器范围:

onDrag(e) {
  if (!this.isDragging) return
  const clientX = e.clientX || e.touches[0].clientX
  let newPosition = clientX - this.startX

  // 获取容器和滑块宽度
  const containerWidth = this.$el.offsetWidth
  const sliderWidth = this.$el.querySelector('.slider').offsetWidth

  // 设置边界
  const minPosition = 0
  const maxPosition = containerWidth - sliderWidth
  newPosition = Math.max(minPosition, Math.min(newPosition, maxPosition))

  this.position = newPosition
  e.preventDefault()
}

添加动画效果

使用 CSS 过渡使滑动更平滑:

.slider {
  transition: transform 0.2s ease;
}

当拖动结束时可以添加回弹效果:

endDrag() {
  this.isDragging = false

  // 如果滑块部分滑出,自动回弹
  const containerWidth = this.$el.offsetWidth
  const sliderWidth = this.$el.querySelector('.slider').offsetWidth

  if (this.position < 0) {
    this.position = 0
  } else if (this.position > containerWidth - sliderWidth) {
    this.position = containerWidth - sliderWidth
  }
}

标签: vue
分享给朋友:

相关文章

vue实现菜单

vue实现菜单

Vue 实现菜单的方法 在 Vue 中实现菜单功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-for 动态生成菜单项 通过数据驱动的方式,利用 v-for 指令动态渲染菜单项。定义一个…

vue实现录播播放

vue实现录播播放

Vue 实现录播播放 使用 video.js 实现 安装 video.js 和相关的 Vue 适配器: npm install video.js @videojs-player/vue 在 Vue…

vue实现多层评论回复

vue实现多层评论回复

Vue 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '父…

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现 安装…

vue实现下载暂停

vue实现下载暂停

Vue实现下载暂停功能 在Vue中实现下载暂停功能,通常需要结合XMLHttpRequest或Fetch API的AbortController来控制请求中断。以下是具体实现方法: 使用XMLHtt…

vue实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提交…