Skip to content

Instantly share code, notes, and snippets.

@shapeshifta78
Last active December 23, 2015 00:09
Show Gist options
  • Save shapeshifta78/6551848 to your computer and use it in GitHub Desktop.
Save shapeshifta78/6551848 to your computer and use it in GitHub Desktop.
Simulate mouseevents for jqueryui slider on mobile devices
$('#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