Last active
August 29, 2015 14:19
-
-
Save reubenmoes/be52204bac6bceb6ca97 to your computer and use it in GitHub Desktop.
Open JS thing with click/esc key close listeners
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
/* | |
* 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