A small library to simplify the handling of drag events on mouse & touch devices.
const Nanodrag = require('nanodrag')
const div = document.createElement('div')
const nd = Nanodrag(div)
nd.on('start', (data) => {
console.log('Drag started!', data)
})
nd.on('move', (data) => {
console.log('Dragging!', data)
})
nd.on('end', (data) => {
console.log('Drag finished!', data)
})
nd.close()
If this is set to true, it will call event.preventDefault()
on the event
provided by the touchmove
or mousemove
event. This property only works
if you did not set {passive: true}
in the constructor.
Examples:
This will attach the listener in passive mode and prevent the calling of
event.preventDefault
regardless of what value Nanodrag.preventDefault
is.
const nd = new Nanodrag(element, {passive: true})
This will attach the listener in active mode allowing you decide to call
event.preventDefault
at a later time.
const nd = new Nanodrag(element)
nd.on('start', () => {
nd.preventDefault = false
})
Create a new nanodrag instance. You can either pass in a valid selector for
.querySelector
or a reference to an HTML element. A nanodrag instance is also
an instance of a nanobus object.
options
trackingDelay
?:number - the delay (in nanoseconds) to wait before turning off the tracking mode if the mouse escapes the tracked element. Default: 300passive
?:boolean - attachtouchmove
andmousemove
as passive listeners. This will prevent callingevent.preventDefault
on move events.
Provide a function to invoke when the specified event is triggered
Attach an event to invoke only once
Invoke an event with a specific payload of data
Remove a specific listener
Remove all listeners for a given event; if no event is specified, removed all listeners.
Remove all listeners on the DOM as well as on the nanobus instances and stop reporting any move events
Triggered when a touch start or mouse down event occur on the nanodrag element.
data
start
:objectx
:number - the x coordinate of the touch instrument or mousey
:number - the y coordinate of the touch instrument or mouse
Triggered when the mouse or touch instrument is moved after being started. For mouse-like devices, this means the button must be actively held down
data
start
:objectx
:number - the starting x coordinate of the touch instrument or mousey
:number - the starting y coordinate of the touch instrument or mouse
current
:objectx
:number - the current x coordinate of the touch instrument or mousey
:number - the current y coordinate of the touch instrument or mouse
direction
:objectx
:string - either 'left' or 'righty
:string - either 'up' or 'down
Triggered when the touchend or mouseup event occurs.
data
start
:objectx
:number - the starting x coordinate of the touch instrument or mousey
:number - the starting y coordinate of the touch instrument or mouse
end
:objectx
:number - the end x coordinate of the touch instrument or mousey
:number - the end y coordinate of the touch instrument or mouse
Copyright © 2018 Todd Kennedy, Apache 2.0 License