draggable-helper
A js library to simplify your drag and drop functions. Start with a element, it will expose hooks(drag, moving, drop). You can stop drag, moving and drop by conditions. You can set minTranslate. Its code is easy, you can check source code.
features
- support touch simplify(single touch)
- to prevent page scrolling when touch
- expose hooks(drag, moving, drop)
- prevent drag and moving by return false in hook
- set min translate to trigger drag
- set the style of dragging element
- set the class of dragging element
- move the element or move a cloned one
- check if event is triggered by mouse left button
- to prevent text be selected when dragging
- Advance usage: bind to parent element, make children element as trigger element or moved element. Check example.
install
npm install draggable-helper
usage & api
import draggableHelper from 'draggable-helper'
const destroy = draggableHelper(HTMLElement dragHandlerEl, Object opt = {})
opt = {
beforeDrag(startEvent, moveEvent, store, opt){
},
drag(startEvent, moveEvent, store, opt){
},
moving: (moveEvent, store, opt) => {
},
drop: (endEvent, store, opt) => {
},
}
store = {
el
originalEl
initialMouse
initialPosition
mouse
move
movedCount
startEvent
endEvent,
restoreDOM()
}
Example
Advance usage: bind to parent element
Advance usage: bind to parent element, make children element as trigger element or moved element.
import draggableHelper from 'draggable-helper'
const destroy = draggableHelper(document.body, {
drag(startEvent, moveEvent, store, opt) {
if (startEvent.target not has class 'your trigger class') {
return false
}
},
getEl: (dragHandlerEl, store, opt) => get the el which will be moved by `store.startEvent.target`
})
prevent drag
In follow case, drag event will be prevented.
- Event target element is follow
const IGNORE_TRIGGERS = ['INPUT','TEXTAREA', 'SELECT', 'OPTGROUP', 'OPTION']
- Event target has class
undraggable
or its ancestor till dragHandlerEl has. - opt.beforeDrag or opt.drag return false
demo
jsfiddle