Created
July 20, 2012 01:36
-
-
Save fregante/3148116 to your computer and use it in GitHub Desktop.
Add class when leaving page
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
//make sure downloads open in another page with target="_blank" | |
//"beforeunload" is fired any time a link is clicked. | |
//If the link points to a file to download, the user won't be actually leaving the page, but beforeunload will still have fired. | |
(function () { | |
var unload = {}; | |
unload.init = function () { | |
//cache html element | |
unload.$html = $(document.documentElement); | |
//attach event | |
$(window).on('beforeunload', unload.unloading); | |
} | |
unload.unloading = function (e) { | |
unload.$html.addClass('unloading'); | |
//set fallback | |
setTimeout(unload.failed, 3000); | |
} | |
//useful if the user comes back, the loading fails, or it takes way too long | |
unload.failed = function () { | |
unload.$html.removeClass('unloading'); | |
} | |
//set this whole thing up on DOMready | |
$(unload.init); | |
return unload; | |
})(); |
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
.unloading .element-to-animate { | |
animation: fade-out 1s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great, thanks!