当前位置:首页 > VUE

vue实现拖拽滚动

2026-03-30 04:40:56VUE

实现拖拽滚动的基本思路

在Vue中实现拖拽滚动功能,可以通过监听鼠标事件并结合CSS样式和JavaScript逻辑来完成。核心是利用鼠标按下、移动和释放事件来触发滚动行为。

使用原生事件监听

创建Vue组件时,在mounted钩子中绑定事件,并在beforeDestroy中解绑以避免内存泄漏。通过clientXclientY记录鼠标位置,计算移动距离来更新滚动位置。

export default {
  data() {
    return {
      isDragging: false,
      startX: 0,
      scrollLeft: 0
    }
  },
  mounted() {
    const element = this.$refs.scrollContainer;
    element.addEventListener('mousedown', this.startDrag);
    element.addEventListener('mousemove', this.drag);
    element.addEventListener('mouseup', this.endDrag);
    element.addEventListener('mouseleave', this.endDrag);
  },
  beforeDestroy() {
    const element = this.$refs.scrollContainer;
    element.removeEventListener('mousedown', this.startDrag);
    element.removeEventListener('mousemove', this.drag);
    element.removeEventListener('mouseup', this.endDrag);
    element.removeEventListener('mouseleave', this.endDrag);
  },
  methods: {
    startDrag(e) {
      this.isDragging = true;
      this.startX = e.clientX;
      this.scrollLeft = this.$refs.scrollContainer.scrollLeft;
    },
    drag(e) {
      if (!this.isDragging) return;
      e.preventDefault();
      const x = e.clientX;
      const walk = (x - this.startX) * 2; // 调整滚动速度
      this.$refs.scrollContainer.scrollLeft = this.scrollLeft - walk;
    },
    endDrag() {
      this.isDragging = false;
    }
  }
}

使用第三方库简化实现

对于复杂场景,可以使用现成的拖拽库如vue-draggablesortablejs。这些库提供了更丰富的API和更好的兼容性。

vue实现拖拽滚动

安装sortablejs

npm install sortablejs

在Vue组件中使用:

vue实现拖拽滚动

import Sortable from 'sortablejs';

export default {
  mounted() {
    const element = this.$refs.scrollContainer;
    Sortable.create(element, {
      animation: 150,
      ghostClass: 'ghost',
      onEnd: (event) => {
        console.log('拖拽完成', event);
      }
    });
  }
}

CSS样式优化

为拖拽容器添加基本样式,确保滚动行为流畅且视觉反馈明确:

.scroll-container {
  overflow-x: auto;
  white-space: nowrap;
  cursor: grab;
  user-select: none;
}

.scroll-container:active {
  cursor: grabbing;
}

.ghost {
  opacity: 0.5;
  background: #c8ebfb;
}

移动端适配

针对触摸设备,需要额外监听touchstarttouchmovetouchend事件。逻辑与鼠标事件类似,但需使用touches数组获取位置信息。

methods: {
  startDrag(e) {
    this.isDragging = true;
    this.startX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
    this.scrollLeft = this.$refs.scrollContainer.scrollLeft;
  },
  drag(e) {
    if (!this.isDragging) return;
    e.preventDefault();
    const x = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
    const walk = (x - this.startX) * 2;
    this.$refs.scrollContainer.scrollLeft = this.scrollLeft - walk;
  }
}

性能优化建议

对于大型列表,使用虚拟滚动技术(如vue-virtual-scroller)减少DOM节点数量。避免在mousemove中执行复杂计算,必要时使用防抖函数限制事件触发频率。

标签: 拖拽vue
分享给朋友:

相关文章

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue原理实现

vue原理实现

Vue 原理实现的核心机制 Vue.js 的核心原理基于响应式系统、虚拟 DOM 和模板编译。以下是其核心实现机制的分解: 响应式系统 Vue 使用 Object.defineProperty(Vu…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…

vue实现尺子

vue实现尺子

Vue 实现尺子组件 实现一个尺子组件可以通过 Canvas 或纯 CSS 完成。以下是两种常见实现方式: 使用 Canvas 绘制尺子 通过 Canvas 动态绘制刻度线和数字,适合需要高精度或动…

vue实现权限

vue实现权限

Vue 实现权限管理的方法 在 Vue 项目中实现权限管理通常涉及路由控制、按钮权限和接口权限。以下是几种常见的实现方式: 路由权限控制 通过动态路由的方式,根据用户权限加载不同的路由配置。可以使用…

vue实现左右菜单联动实现

vue实现左右菜单联动实现

Vue 实现左右菜单联动 使用事件总线通信 在 Vue 中可以通过事件总线实现左右菜单的通信。创建一个中央事件总线实例,让左右菜单通过事件监听和触发实现联动。 // eventBus.js impo…