mdrag
no dependancy, minimum but strong drag lib for both dom and virtual dom support
Install
NodeJS
:
npm install -S mdrag
Browser
:
unpkg
Manually
-
download
dist/mdrag.iife.js
file into your folder -
included
mdrag.iife.js
in your html, no dependancy
Usage
use with DOM
// create any div, or exists...var clip = documentclipstylecssText = "position:absolute; z-index:999; border:1px dotted red"documentbody var clipDrag = ; // param can be empty or some options {...} documentbodyonmousedown =
This way you can do any thing using javascript, like boundary check, etc.
use with V-DOM(e.g. mithril)
API
mdrag([options]) => downHandler
create drag root factory, return a function that can be handler for mousedown etc.
options is js object, can be following key:
revertOnFail
: boolean(default true), when move event returnfalse
, mdrag will stop drag then, if this option istrue
, will revert to previous datatouch
: boolean(default auto detect), if false, will force using mouse event instead of touch event through the browser hastouchmove
event. useful in phantom etc.
{downHandler}( name, [userdata], moveFunc, upFunc )
the handler function returned from mdrag, which can be using as handler for onmousedown/touchstart or anything you want to trigger a drag
name
: string, save internally to back reference, this will allow you to get drag status at any time, make 2 drag interact etc.userdata
: object, that can pass in customer key&val, can accessable usingdata.user
inmoveFunc
andupFunc
function- onmousedown: special property for
userdata
, which will invoked whendownevent
happens, can be used to hook the downevent.
- onmousedown: special property for
moveFunc
: function, with 3 parameters( evt, data, root )- evt is mousemove/touchmove event object
- data is object, as below
ox: number // mouse position (pageX) when dragstart (e.g. mousedown)oy: number // mouse position (pageY) when dragstart (e.g. mousedown)dx: number // mouse position difference with ox when move occur (e.g. mousemove)dy: number // mouse position difference with oy when move occur (e.g. mousemove)pageX: number // pageX from the eventpageY: number // pageY from the eventuser: object // user data passed with param `userdata`, see above; if omit, this will be {}upFunc
: function, parameters same asmoveFunc
{downHandler}.destroy()
destroy the drag instance, and remove all event listener
Tests
Using ptest
to test with phantomjs
, run pre-recorded action, make snapshot, and then compare with right snapshot image.
If the two image is same, test passed, else test failed.
ptest
is another lib that still WIP, will open source at some later time.
Copyright
MIT