Created
June 17, 2018 22:19
-
-
Save IamManchanda/2ad0c95acb52a63975ae58518b5243c2 to your computer and use it in GitHub Desktop.
jQuery Mouse and Keyboard events
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
$(document).ready(() => { | |
const onEvent = (event, eventLog = event) => { | |
let box; | |
let pageCoords; | |
if (event.startsWith('key')) { | |
box = $(document); | |
pageCoords = (e) => `(${e.which})`; | |
} else { | |
box = $('.element'); | |
pageCoords = (e) => `(${e.pageX}, ${e.pageY})`; | |
} | |
return box.on(event, (e) => { | |
console.log(eventLog, pageCoords(e)); | |
}); | |
}; | |
onEvent('click', 'You just clicked the box'); | |
onEvent('dblclick', 'You just double clicked the box'); | |
onEvent('mouseenter', 'You entered the box'); | |
onEvent('mouseleave', 'You just left the box'); | |
onEvent('mousedown', 'You just pressed the mouse'); | |
onEvent('mouseup', 'You just released the mouse'); | |
onEvent('mousemove', 'You are moving the mouse'); | |
onEvent('keydown', 'You just pressed the key'); | |
onEvent('keyup', 'You just released the key'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment