Skip to content

Instantly share code, notes, and snippets.

@newshorts
Created August 20, 2015 23:24
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 newshorts/403bd6dc2fb47f3d7a5d to your computer and use it in GitHub Desktop.
Save newshorts/403bd6dc2fb47f3d7a5d to your computer and use it in GitHub Desktop.
A small script to show and hide an element based on the event of css transition end
var COVER;
var Cover;
(function($) {
COVER = function() {
// private
var __ = this;
var $cover = $('#cover');
var isShowing = false;
// public
__.show = function(callback) {
isShowing = true;
return (function() {
$cover.on('transitionend', function(evt) {
if(isShowing) {
console.log('showing ended');
if(callback) {
callback()
}
}
});
$cover.css({ display: 'block'});
setTimeout(function() { $cover.css({ opacity: 1 }); }, 100);
})();
};
__.hide = function(callback) {
isShowing = false;
return (function() {
$cover.on('transitionend', function(evt) {
if(!isShowing) {
console.log('hiding ended');
$cover.css({display: 'none'});
if(callback) {
callback();
}
}
});
$cover.css({ opacity: 0 });
})();
};
return __;
};
Cover = new COVER();
})(jQuery);
setTimeout(function() {
Cover.hide(function() {
console.log('called back');
});
}, 1000);
setTimeout(function() {
console.log('showing')
Cover.show(function() { console.log('called back again') });
}, 3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment