当前位置:首页 > VUE

vue实现拖拽滚动

2026-01-17 11:29:00VUE

Vue 实现拖拽滚动的方法

在 Vue 中实现拖拽滚动功能,可以通过监听鼠标事件并结合 CSS 或 JavaScript 控制滚动行为。以下是几种常见的实现方式:

使用原生事件监听

通过监听 mousedownmousemovemouseup 事件,结合 scrollLeftscrollTop 属性实现拖拽滚动。

<template>
  <div
    ref="scrollContainer"
    class="scroll-container"
    @mousedown="startDrag"
    @mousemove="onDrag"
    @mouseup="endDrag"
    @mouseleave="endDrag"
  >
    <div class="content">
      <!-- 可滚动内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDragging: false,
      startX: 0,
      startY: 0,
      scrollLeft: 0,
      scrollTop: 0,
    };
  },
  methods: {
    startDrag(e) {
      this.isDragging = true;
      this.startX = e.pageX;
      this.startY = e.pageY;
      this.scrollLeft = this.$refs.scrollContainer.scrollLeft;
      this.scrollTop = this.$refs.scrollContainer.scrollTop;
    },
    onDrag(e) {
      if (!this.isDragging) return;
      const x = e.pageX;
      const y = e.pageY;
      const walkX = (this.startX - x) * 2; // 调整拖拽灵敏度
      const walkY = (this.startY - y) * 2;
      this.$refs.scrollContainer.scrollLeft = this.scrollLeft + walkX;
      this.$refs.scrollContainer.scrollTop = this.scrollTop + walkY;
    },
    endDrag() {
      this.isDragging = false;
    },
  },
};
</script>

<style>
.scroll-container {
  overflow: auto;
  cursor: grab;
}
.scroll-container:active {
  cursor: grabbing;
}
</style>

使用第三方库

如果需要更复杂的拖拽滚动功能,可以使用第三方库如 vue-draggablesortablejs

安装 sortablejs

npm install sortablejs

在 Vue 中使用:

<template>
  <div ref="scrollContainer" class="scroll-container">
    <div class="content">
      <!-- 可拖拽滚动的内容 -->
    </div>
  </div>
</template>

<script>
import Sortable from 'sortablejs';

export default {
  mounted() {
    new Sortable(this.$refs.scrollContainer, {
      animation: 150,
      onStart: () => {
        this.$refs.scrollContainer.style.cursor = 'grabbing';
      },
      onEnd: () => {
        this.$refs.scrollContainer.style.cursor = 'grab';
      },
    });
  },
};
</script>

使用 CSS 属性

对于简单的拖拽滚动效果,可以直接使用 CSS 的 scroll-snap 属性实现平滑滚动。

vue实现拖拽滚动

<template>
  <div class="scroll-container">
    <div class="content">
      <!-- 内容区块 -->
    </div>
  </div>
</template>

<style>
.scroll-container {
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  cursor: grab;
}
.content {
  display: flex;
  scroll-snap-align: start;
}
</style>

注意事项

  • 拖拽滚动时需注意性能问题,避免频繁触发 mousemove 事件。
  • 移动端适配可能需要额外处理触摸事件(touchstarttouchmovetouchend)。
  • 拖拽过程中可以添加过渡动画提升用户体验。

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

相关文章

vue怎么实现数据检测

vue怎么实现数据检测

Vue 数据检测的实现方式 Vue 通过响应式系统实现数据检测,核心机制基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3)。以下是具体实现方法: 基于 Vue…

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…

vue实现type切换

vue实现type切换

Vue 实现 Type 切换的实现方法 在 Vue 中实现 Type 切换功能可以通过动态组件、条件渲染或路由切换等方式实现。以下是几种常见方法: 使用 v-if 或 v-show 条件渲染 通过绑…

vue实现动态禁用

vue实现动态禁用

动态禁用表单元素或按钮 在Vue中实现动态禁用功能通常通过v-bind:disabled(或简写为:disabled)绑定一个响应式变量实现。当变量值为true时,元素被禁用;为false时启用。…