m_drag
no dependancy, minimum but strong drag lib for both dom and virtual dom support
Install
npm install -S m_drag
<script type="text/javascript" src="js/m_drag.js"></script>
Usage
use with DOM
var clip = document.createElement('div')
clip.style.cssText = "position:absolute; z-index:999; border:1px dotted red"
document.body.appendChild(clip)
var clipDrag = m_drag({});
document.body.onmousedown =
clipDrag(
'clip',
function(e, data){
clip.style.left = data.ox + 'px'
clip.style.top = data.oy + 'px'
clip.style.width = -data.dx + 'px'
clip.style.height = -data.dy + 'px'
},
function(e, data){
console.log('drag complete..')
}
)
This way you can do any thing using javascript, like boundary check, etc.
use with V-DOM(e.g. mithril)
m('div.draggable',
{onmousedown:
clipDrag(
'clip2',
{ onmousedown:function(){ m.redraw.strategy('none') } },
function(evt, data){ },
function(evt, data){ }
)
}
)
API
m_drag([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 return false
, m_drag will stop drag then, if this option is true
, will revert to previous data
touch
: boolean(default auto detect), if false, will force using mouse event instead of touch event through the browser has touchmove
event. useful in phantom etc.
{downHandler}( name, [userdata], moveFunc, upFunc )
the handler function returned from m_drag, which can be using as handler for onmousedown/touchstart or anything you want to trigger a drag
{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