Skip to content

Instantly share code, notes, and snippets.

@reubenmoes
Last active August 29, 2015 14:19
Show Gist options
  • Save reubenmoes/be52204bac6bceb6ca97 to your computer and use it in GitHub Desktop.
Save reubenmoes/be52204bac6bceb6ca97 to your computer and use it in GitHub Desktop.
Open JS thing with click/esc key close listeners
/*
* Close the thing and bind close listeners
*/
function closeThing(){
//Do some stuff
//Unbind close listeners
$(document).off('keyup.closeThing').off('click.closeThing');
}
/*
* Open the thing and bind close listeners
*/
function openThing(){
//Do some stuff
//Bind Close listeners
$(document)
.on('click.closeThing', function (e){
//click was not on the drawerDrigger or within the drawer
//Might need some logic to only close on clicks outside of an element
//if(!$drawerTrigger.is(e.target) && !$drawer.is(e.target) && !$drawer.has(e.target).length){
closeThing();
})
.on('keyup.closeThing', function (e){
//esc
if (e.keyCode === 27) {
closeThing();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment