jQuery Drag element on mobile

Using the touchmove event to drag an element with a single touch.
$('#my-element').get(0).ontouchstart = function(e) { e.preventDefault(); }
$('#my-element').get(0).ontouchmove = function(e){
	  if(e.touches.length == 1){ // single finger touch
	    var touch = e.touches[0];
	    $('#my-element').css({ left: (touch.pageX) + "px", top: (touch.pageY)+ "px" }); 
	  }
}
Posted by
Snippet Viewed 1722 times.

Share your jQuery code snippets:

  • Get some recognition & a link back to your site.
  • Create your own code library.
  • Help your fellow developers, as they have helped you.

 

Submit | Browse

Most Recent jquery snippets

Most Viewed