Login / Join
Mapping Touch Events to Mouse Events
submitted on 03/03/2013 by
Codicode
+ infos
Config
Embed
Share
Mapping Touch Events to Mouse Events
by
Codicode
This sample code shows how to map touch events to mouse events.
Author : Chtiwi Malek
http://www.codicode.com
Framework 1 :
-- None --
jQuery 2.0.3
jQuery 1.9.1
jQuery 1.9.0
jQuery 1.8.0
jQuery 1.7.1
jQuery 1.6.3
jQuery UI 1.10.3
MooTools 1.4.5
AngularJS 1.0.4
Prototype 1.7.1.0
Dojo 1.8.3
Chrome Frame 1.0.3
Ext Core 3.1.0
SWFObject 2.2
WebFont Loader 1.1.2
Framework 2 :
-- None --
jQuery 2.0.3
jQuery 1.9.1
jQuery 1.9.0
jQuery 1.8.0
jQuery 1.7.1
jQuery 1.6.3
jQuery UI 1.10.3
MooTools 1.4.5
AngularJS 1.0.4
Prototype 1.7.1.0
Dojo 1.8.3
Chrome Frame 1.0.3
Ext Core 3.1.0
SWFObject 2.2
WebFont Loader 1.1.2
+ Js file(s) :
+ Css file(s) :
Apply
Link :
Direct http link :
Fork button :
Html code :
Embed preview :
Copy the following html code to your page to embed the preview,
To include many previews on a single page, just repeat the first line.
You can change the width, height and border color (bcolor).
<div class="refork-widget" code="167" style="width:100%;height:200px;" bcolor="#ccc"></div>
<script type="text/javascript" src="//snippet.run/d/z.js"></script>
HTML
<div id="caree"><div>
CSS
body { background-color:#000; color:#fff; margin:10px; } #caree { position:absolute; width:80px; height:80px; background-color:#fff; border-radius:40px; }
Javascript
var isDown = false; $('#caree').mousedown(function (e) { isDown = true; }); $('html').mouseup(function (e) { isDown = false; }); $('html').mousemove(function (e) { if (isDown) { $('#caree').offset({ top: e.pageY-40, left: e.pageX-40 }); } }); 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(); } document.addEventListener("touchstart", touch2Mouse, true); document.addEventListener("touchmove", touch2Mouse, true); document.addEventListener("touchend", touch2Mouse, true);
©2013 Copyright snippet.run
Privacy
-
Terms of use
-
About
-
Contact