Last active
December 23, 2015 00:09
-
-
Save shapeshifta78/6551848 to your computer and use it in GitHub Desktop.
Simulate mouseevents for jqueryui slider on mobile devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#slider').on('touchstart touchmove touchend', 'a.ui-slider-handle', function (event) { | |
//mousevents to simulate on touch | |
var touchEvents = { | |
touchstart: 'mousedown', | |
touchmove: 'mousemove', | |
touchend: 'mouseup' | |
}; | |
//get element and type of event to simulate | |
var simulatedEvent = touchEvents[event.originalEvent.type], | |
touch = event.originalEvent.changedTouches[0], | |
mouseEvent = document.createEvent('MouseEvent'); | |
//return if no mouse event is matched | |
if (typeof simulatedEvent === 'undefined') { | |
return; | |
} | |
//init the mousevent if we have a matched event | |
mouseEvent.initMouseEvent( | |
simulatedEvent, //type | |
true, //bubbles | |
true, //cancelable | |
window, //view | |
1, //detail | |
touch.screenX, //screenX | |
touch.screenY, //screenY | |
touch.clientX, //clientX | |
touch.clientY, //clientY | |
false, //ctrlKey | |
false, //altKey | |
false, //shiftKey | |
false, //metaKey | |
0, //button | |
null //related target | |
); | |
//dispatch event on touched element | |
touch.target.dispatchEvent(mouseEvent); | |
event.preventDefault(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment