Skip to content

Instantly share code, notes, and snippets.

@ksk1015
Created March 1, 2017 07:04
Show Gist options
  • Save ksk1015/36fa95bd654af2320ea5be5593541b0f to your computer and use it in GitHub Desktop.
Save ksk1015/36fa95bd654af2320ea5be5593541b0f to your computer and use it in GitHub Desktop.
detect movable mouse (means a device has movable mouse pointer)
(function(){
// over 2 mousemove events fired in 300ms
var over = 2, period = 300;
var count = 0;
var timer = null;
var mousemove = function(evt){
if ( !timer ) {
timer = setTimeout(function(){
timer = null;
count = 0;
}, period);
return;
}
if ( ++count < over ) {
return;
}
document.removeEventListener('mousemove', mousemove);
window.movableMouse = true;
document.documentElement.classList.add('movableMouse');
fireEvt();
};
var fireEvt = function(){
var evt = document.createEvent('HTMLEvents');
evt.initEvent('movableMouse', false, false);
document.dispatchEvent(evt);
};
window.movableMouse = false;
document.addEventListener('mousemove', mousemove);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment