Created
August 20, 2015 23:24
-
-
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
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 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