Last active
August 29, 2015 14:13
-
-
Save ruslanchek/b62616ccd7c9979a0ce7 to your computer and use it in GitHub Desktop.
Click outside JS class
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
var UIClickOutside = function(container, onClickOutside){ | |
var $container = $(container); | |
this.bind = function(){ | |
$(document).on('mouseup.UIClickOutside', function (e){ | |
if (!$container.is(e.target) && $container.has(e.target).length === 0){ | |
if(onClickOutside) onClickOutside(e.target); | |
} | |
}); | |
}; | |
this.unbind = function(){ | |
$(document).off('mouseup.UIClickOutside'); | |
}; | |
}; | |
// Usage example | |
var clickOutside = new UIClickOutside('.popup', function($clickedTarget){ | |
// Hide your block or anything else... | |
}); | |
// Activate | |
clickOutside.bind(); | |
// Deactivate | |
clickOutside.unbind(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment