touch - jquery apply momentum of inertia on drag -
i write little script drag div container. works touch , mouse events.
i have difficulties write momentum script in order have container moving after touchend. know how calculate momentum don't know right way implement it...
here script :
var divtodrag = $('.container'); var divondrag = $(document); var dragging = false; var swipe = false; var scrolly = false; var threshold = {x:30, y:10}; var swipeleft = false; var swiperight = false; var swipeup = false; var swipedown = false; var threshx = false; var threshy = false; var maxspeed = 5; function prefix() { styles = window.getcomputedstyle(document.documentelement, ''), pre = (array.prototype.slice .call(styles) .join('') .match(/-(moz|webkit|ms)-/) || (styles.olink === '' && ['', 'o']) )[1], dom = ('webkit|moz|ms|o').match(new regexp('(' + pre + ')', 'i'))[1]; } prefix(); function pointereventxy(e) { out = {x:0, y:0}; if(e.type == 'touchstart' || e.type == 'touchmove' || e.type == 'touchend'){ touch = e.originalevent.touches[0] || e.originalevent.changedtouches[0]; out.x = touch.pagex; out.y = touch.pagey; } else if (e.type == 'mousedown' || e.type == 'mouseup' || e.type == 'mousemove') { out.x = e.pagex; out.y = e.pagey; } return out; }; divondrag.on('mousedown touchstart', function(event){ dragging = true; divtodrag.stop(); pointereventxy(event); startcoordsx = prevcoordsx = out.x; startcoordsy = prevcoordsy = out.y; divcoordsx = divtodrag.position().left; divcoordsy = divtodrag.position().top; initialcoordsx = startcoordsx - divcoordsx; initialcoordsy = startcoordsy - divcoordsy; divtodrag.data("mouseevents",[event]); }) divondrag.on('mousemove touchmove', function(event){ if (dragging == true) { pointereventxy(event); currentcoordsx = out.x; currentcoordsy = out.y; xthreshold = math.abs(currentcoordsx - startcoordsx) ythreshold = math.abs(currentcoordsy - startcoordsy) var mouseevents = divtodrag.data( "mouseevents" ); if ((event.timestamp - mouseevents[ mouseevents.length - 1 ].timestamp ) > 40){ mouseevents.push(event); if (mouseevents.length > 2){ mouseevents.shift(); } } if(xthreshold > threshold.x && ythreshold < threshold.y && scrolly == false || swipe == true ) { event.preventdefault(); swipe = true; dragdirection(); x = currentcoordsx - initialcoordsx - threshx; y = currentcoordsy - initialcoordsy - threshy; divtodrag.attr('style', '-'+pre+'-transition: 0s ease-in !important; -'+pre+'-transform: translate3d('+x+'px, 0px, 0px)'); } else if (xthreshold < threshold.x && ythreshold > threshold.y) { scrolly = true; } } }) divondrag.on('mouseup touchend', function(event){ dragging = false; scrolly = false; swipe = false; swipeleft = false; swiperight = false; swipeup = false; swipedown = false; momentum(); }) function dragdirection() { if (prevcoordsx < currentcoordsx) { swiperight = true; threshx = threshold.x; } else if (prevcoordsx > currentcoordsx) { swipeleft = true; threshx = -threshold.x; } if (prevcoordsy < currentcoordsy) { swipedown = true; threshy = threshold.y; } else if (prevcoordsy > currentcoordsy) { swipeup = true; threshy = -threshold.y; } if (swiperight == true && swipeleft == true || swipeup == true && swipedown == true) { threshx = 0; threshy = 0; prevcoordsx = currentcoordsx - initialcoordsx; prevcoordsy = currentcoordsy - initialcoordsy; } prevcoordsx = currentcoordsx; prevcoordsy = currentcoordsy; } function momentum() { var lastevent = divtodrag.data( "mouseevents" ).shift(); if (!lastevent){ return; } var deltax = (event.pagex - lastevent.pagex); var deltay = (event.pagey - lastevent.pagey); var deltams = math.max((event.timestamp - lastevent.timestamp),1); var speedx = math.max(math.min( (deltax / deltams), maxspeed ),-maxspeed); var speedy = math.max(math.min( (deltay / deltams), maxspeed ),-maxspeed); var laststeptime = new date(); divtodrag.animate( {textindent: 0}, {duration: (math.max(math.abs( speedx ), math.abs( speedy )) * 3000), step: function( currentstep ){ speedx *= (currentstep / 100); speedy *= (currentstep / 100); var = new date(); var stepduration = (now.gettime() - laststeptime.gettime()); laststeptime = now; var position = divtodrag.position(); var newleft = (position.left + (speedx * stepduration)); var newtop = (position.top + (speedy * stepduration)); console.log(newleft) divtodrag.css({transform: 'translatex('+newleft+'px)'}); } } ); } and fiddle : http://jsfiddle.net/v5jmu/2/
update
i made new fiddle seems work better matjazek answer
i recommend solve problem different aproach, if insist in using method, should job done:
you can set drag coeficient with
var drag=0.95; // drag coeficient
Comments
Post a Comment