Skip to content

Enable scrolling of <draggable> container while dragging an element #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PablitoDh opened this issue Nov 20, 2024 · 4 comments
Closed

Comments

@PablitoDh
Copy link

It is not possible to scroll the container with the mouse while dragging an element with the mouse. Is there any way to enable this?

@phphe
Copy link
Owner

phphe commented Nov 20, 2024

no. It use HTML Drag and Drop API. Their behavior is same.

@PablitoDh
Copy link
Author

Why did it work in the previous version, but it doesn't work in this one?

@phphe
Copy link
Owner

phphe commented Nov 20, 2024

do you mean he-tree-vue? It does not use HTML Drag and Drop API.

@johannesss
Copy link

johannesss commented Mar 26, 2025

I've implemented this, see example:

<template>
  <Draggable />
</template>

<script>
import '@he-tree/vue/style/default.css';
import { Draggable } from '@he-tree/vue';

export default {
  data() {
    return {
      stopDragScrollY: true
    }
  },

  computed: {
    isVerticalMaxed() {
      return window.innerHeight + window.scrollY >= document.body.offsetHeight;
    }
  },
  
  mounted() {
    window.addEventListener('drag', this.handleDrag);
  },
  
  unmounted() {
    window.removeEventListener('drag', this.handleDrag);
  },
  
  methods: {
    handleDrag(event) {
      this.stopDragScrollY = true;
      
      if (event.clientY < 150) {
        this.stopDragScrollY = false;
        this.handleScroll(-1); // scroll up
      }
      
      if (event.clientY > document.documentElement.clientHeight - 150 && !this.isVerticalMaxed) {
        this.stopDragScrollY = false;
        this.handleScroll(1); // scroll down
      }
    },
    
    handleScroll(stepY) {
      const scrollY = document.documentElement.scrollTop || document.body.scrollTop;
      window.scrollTo(0, scrollY + stepY);
      
      // continue scrolling until vertical direction is maxed using requestAnimationFrame
      if (!this.stopDragScrollY) {
        requestAnimationFrame(() => this.handleScroll(stepY));
      }
    },        
  },
}
</script>

@phphe phphe closed this as completed Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants