Skip to content

Instantly share code, notes, and snippets.

@jaouadballat
Created June 6, 2019 22:31
Show Gist options
  • Save jaouadballat/98d27599f910d28ab93f25ff7977b84e to your computer and use it in GitHub Desktop.
Save jaouadballat/98d27599f910d28ab93f25ff7977b84e to your computer and use it in GitHub Desktop.
class Draggable {
constructor() {
this.container = document.querySelector('.box__dragabble');
this.box = document.querySelectorAll('.box');
this._addEventListener();
}
_addEventListener() {
this.box.forEach(element => {
element.addEventListener('dragenter', this.dragenter)
element.addEventListener('dragleave', this.dragleave)
element.addEventListener('dragover', this.dragover)
element.addEventListener('drop', this.drop)
});
this.container.addEventListener('dragstart', this.dragstart);
this.container.addEventListener('dragend', this.dragend);
}
dragstart(e) {
this.classList.add('drag_start');
setTimeout(() => {
this.classList.add('invisible');
}, 0);
}
dragend(e) {
console.log('dragend')
this.classList.remove('invisible');
this.classList.remove('drag_start');
}
dragenter(e) {
e.preventDefault();
console.log('dragenter')
this.classList.add('drag_enter');
}
dragleave(e) {
console.log('dragleave')
this.classList.remove('drag_enter');
}
dragover(e) {
e.preventDefault();
console.log('dragover')
}
drop() {
let container = document.querySelector('.box__dragabble');
this.classList.remove('drag_enter')
this.append(container)
}
static init() {
return new this();
}
}
document
.addEventListener('load', Draggable.init());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment