Skip to content

Instantly share code, notes, and snippets.

@r4fx
Last active August 29, 2015 14:15
Show Gist options
  • Save r4fx/ffe6386ba4b37db9af38 to your computer and use it in GitHub Desktop.
Save r4fx/ffe6386ba4b37db9af38 to your computer and use it in GitHub Desktop.
Uniwersal hider
// Uniwersal hider
// ============================================================
function hider(targetToHide, trigger) {
$(document).on('mouseup', function (e) {
if (!targetToHide.is(e.target) // if the target of the click isn't the container...
&& targetToHide.has(e.target).length === 0 // ... nor a descendant of the container
&& !trigger.is(e.target)) // ... nor a trigger of the container
{
console.log(targetToHide);
targetToHide.add(trigger).removeClass('active');
}
$(document).unbind('mouseup'); // Don't ever keep listening for clicks !
});
}
// Use case
function hamburgerMenu() {
var $container = $("#top-menu");
var $trigger = $('.top-menu-trigger');
$trigger.on('click', function () {
$(this).add($container).toggleClass('active');
hider($container, $trigger);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment