html - Pure javascript drag not smooth on iPad -


i trying drag div (#drag) in it's parent (#container) pure javascript only.
(only need work on ipad).

i wrote script work fine when use in chrome "emulate touch events" turned on,
but on real ipad, start dragging little fast div stop following.

i thought finger might out of element when move fast,
so set addeventlistner on body instead of div, still same.

anyone have idea why? , how make work smoothly on ipad?

demo: http://jsfiddle.net/kxrez/

javascript

var dom = {     container: document.getelementbyid("container"),     drag: document.getelementbyid("drag"), } var container = {     x: dom.container.getboundingclientrect().left,     y: dom.container.getboundingclientrect().top,     w: dom.container.getboundingclientrect().width,     h: dom.container.getboundingclientrect().height } var drag = {     w: dom.drag.offsetwidth,     h: dom.drag.offsetheight }  target = null;  document.body.addeventlistener('touchstart', handletouchstart, false); document.body.addeventlistener('touchmove', handletouchmove, false); document.body.addeventlistener('touchend', handletouchend, false);  function handletouchstart(e) {     if (e.touches.length == 1) {         var touch = e.touches[0];         target = touch.target;     } } function handletouchmove(e) {     if (e.touches.length == 1) {         if(target ===  dom.drag) {             movedrag(e);         }     } } function handletouchend(e) {     if (e.touches.length == 0) { // user took last finger off screen         target = null;     } }  function movedrag(e) {     var touch = e.touches[0];     var posx = touch.pagex - container.x - drag.w / 2;     var minx = 0;     var maxx = container.w - drag.w;     if(posx < minx) {posx = minx;}      else if(posx > maxx) {posx = maxx;}     var posy = touch.pagey - container.y - drag.h / 2;     var miny = 0;     var maxy = container.h - drag.h;     if(posy < miny) {posy = miny;}      else if(posy > maxy) {posy = maxy;}     dom.drag.style.left = posx + "px";     dom.drag.style.top = posy + "px"; } 

you might consider moving lot of variables you're using in movedrag in static memory pool (see: http://www.html5rocks.com/en/tutorials/speed/static-mem-pools/), since function called quite often. also, changing offset using style.left , style.top requires reflow each finger move - i'd suggest trying using render transforms during move , updating styles in handletouchend


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -