Skip to content

Instantly share code, notes, and snippets.

@monkishtypist
Last active December 13, 2016 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkishtypist/00f7e9620615aad5c1fb4052359bfb48 to your computer and use it in GitHub Desktop.
Save monkishtypist/00f7e9620615aad5c1fb4052359bfb48 to your computer and use it in GitHub Desktop.
Bootstrap Exit Pop
/* Simple Bootstrap jQuery Exit Pop
* Trigger an existing Bootstrap modal on mouseleave()
*/
/* This first version uses a variable to determine when to trigger pop. */
(function(){
var pop = false; /* it has not pop'd */
jQuery(document).mouseleave(function(){
if(!pop || typeof pop === 'undefined'){
jQuery('#myModal').modal('show'); /* show the modal */
pop = true; /* it has pop'd */
};
});
})(jQuery);
/* This second version unsets `mouseleave()` once it has been triggered. */
(function(){
jQuery(document).on('mouseleave', function(){
jQuery('#myModal').modal('show');
jQuery(document).off('mouseleave'); /* unset the trigger */
});
})(jQuery);
/* This third version checks that the mouse has left at the top. This prevent scrollbar triggering exit pop. */
(function(){
jQuery(document).on('mouseleave', function(e){
if(e.clientY < 0){ /* only trigger if leave top */
jQuery('#myModal').modal('show');
jQuery(document).off('mouseleave');
}
});
})(jQuery);
@monkishtypist
Copy link
Author

monkishtypist commented Dec 13, 2016

Example:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment