Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Open layer, then close layer by clicking document.
// TODO: Add complete HTML document.
$layer: $('.layer'),
$button: $('.button'),
_initialize: function() {
$(document).on('click', this._onClick.bind(this));
},
_onClick: function(event) {
var $target = $(event.target),
isLayer = $target.is(this.$layer) || $target.parents().is(this.$layer),
isButton = ($target.is(this.$button) || $target.parents().is(this.$button));
if (isLayer) {
return;
}
if (isButton && !this.$layer.is(':visible')) {
this.$layer.show();
} else {
this.$layer.hide();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment