Skip to content

Instantly share code, notes, and snippets.

@abovedave
Created August 11, 2014 15:53
Show Gist options
  • Save abovedave/5aa6409633aca91cfc7a to your computer and use it in GitHub Desktop.
Save abovedave/5aa6409633aca91cfc7a to your computer and use it in GitHub Desktop.
// ------------------------
// Syntax 'input:class:target' for toggling classes
// E.g., data-class="body:show-nav:header nav"
$('[data-class]').each(function(){
var self = $(this),
input = self.data('class').split(':'),
root = $(input[0]),
theclass = input[1],
target = $(input[2]),
transtionend = 'webkitTransitionEnd otransitionend mstransitionEnd transitionend';
if (input && input.length > 1){
self.on('click', function(){
if (root.hasClass(theclass)) {
root.removeClass(theclass+'-animate');
target.one(transtionend, function(){
root.removeClass(theclass);
});
} else {
target.unbind(transtionend);
root.addClass(theclass);
// Cheap and nasty :S
setTimeout(function () {
root.addClass(theclass+'-animate');
}, 20);
}
return false;
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment