Category : Javascript | Author : Chtiwi Malek | First posted : 3/3/2013 | Updated : 9/26/2017
Tags : touch, drag, javascript, mouse, website, ux, ui
Add touch screen support to your website (The easy way)

Add touch screen support to your website (The easy way)

In this article we will add touch support to an existing web application simply by mapping touch events to existing mouse events handlers. If you are already handling mouse events just copy and paste this code.
 
First we need to listen to touch events and call a function 'touch2Mouse' each time a touch event is fired:
document.addEventListener("touchstart", touch2Mouse, true);
document.addEventListener("touchmove", touch2Mouse, true);
document.addEventListener("touchend", touch2Mouse, true);
The following function will initiate and fire mouse events (event.initMouseEvent), for each touch events type we'll fire the equivalent mouse event :

Touchstart  =>  Mousedown
Touchend  =>  Mouseup
Touchmove  =>  Mousemove

and then we will cancel the original touch event using preventdefault :
function touch2Mouse(e)
{
  var theTouch = e.changedTouches[0];
  var mouseEv;

  switch(e.type)
  {
    case "touchstart": mouseEv="mousedown"; break;  
    case "touchend":   mouseEv="mouseup"; break;
    case "touchmove":  mouseEv="mousemove"; break;
    default: return;
  }

  var mouseEvent = document.createEvent("MouseEvent");
  mouseEvent.initMouseEvent(mouseEv, true, true, window, 1, theTouch.screenX, theTouch.screenY, theTouch.clientX, theTouch.clientY, false, false, false, false, 0, null);
  theTouch.target.dispatchEvent(mouseEvent);

  e.preventDefault();
}
Just follow the same logic to wire other events (mouseover, click, dblclick, ...)

This should work on all browsers and devices, personally I tested it successfully on IE, Firefox, Google Chrome, Opera, Firefox for android, Chrome for android, Opera for android and iPhone.

A full working example can be tested here.

Hope you like this and don't hesitate to comment, Thanks.
About the author :
Malek Chtiwi is the man behind Codicode.com
34 years old full stack developer.
Loves technology; but also likes design, photography and composing music.
Comments & Opinions :
great
works very good, thanks.
- by Joe on 3/5/2013
Integration with good old hotpotatoes exercises
Is it possible to integrate the touch support to old hotpot quizzes like this:
http://hotpot.uvic.ca/wintutor6/jmix2.htm
I tried and tried, but unfortunately my knowledge is not good enough.
Please help me!

Thanks!
- by Daniel Tebs on 3/7/2013
Works well with Hot Potatoes - now in the 21st Century
This is great!!! Drag and drop works now in Hot Potatoes on both iPad and Android devices as well as Win8 touch screen. I just need to get the onClick etc working to usse the buttons and drop-down menues.
Thanks very much. Keep up the good work.
- by Malcolm Hay on 11/29/2013
Where did you insert this code into the Hot Potatoes css file? Does it matter where you put it? Thx.
- by Sue Hellman on 6/25/2014
Just wanted to say Hello.
Great post. Website: http://herb24.space
- by Great post. Website: http://herb24.space on 9/23/2017
Leave a Comment:
Name :
Email : * will not be shown
Title :
Comment :


» Member access