public
Last active

JqFxStack - jQuery helper for easy reversion of element properties

  • Download Gist
JqFxStack.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/*
AUTHOR: Tomasz Zielinski
LICENSE: public domain where appropriate; free for everyone, for all usages, elsewhere
*/
 
function JqFxStack() {
this.id_map = {};
}
JqFxStack.prototype._fx = function(func_name, element, css, options) {
var prev_css = {};
for(var css_prop in css) {
if(css.hasOwnProperty(css_prop)) {
prev_css[css_prop] = element.css(css_prop);
}
}
var fx = {
func_name: func_name,
css: css,
prev_css: prev_css,
options: options
};
var element_id = element.attr('id');
if(!this.id_map.hasOwnProperty(element_id)) {
this.id_map[element_id] = [];
}
this.id_map[element_id].push(fx);
 
element[fx.func_name](fx.css, fx.options);
};
 
JqFxStack.prototype.revert = function(element) {
if(element instanceof Array) {
var fx_stack = this;
$.each(element, function(idx, elem){
fx_stack.revert(elem);
});
} else {
var element_id = element.attr('id');
var fx = this.id_map[element_id].pop();
element[fx.func_name](fx.prev_css, fx.options);
}
return this;
};
JqFxStack.prototype.revert_all = function(element) {
if(element instanceof Array) {
var fx_stack = this;
$.each(element, function(idx, elem){
fx_stack.revert_all(elem);
});
} else {
var element_id = element.attr('id');
var len = this.id_map[element_id].length;
for(var i=0; i<len; i++) {
this.revert(element);
}
}
return this;
};
 
JqFxStack.prototype.animate = function(element, css, options) {
this._fx('animate', element, css, options);
return this;
};
JqFxStack.prototype.css = function(element, css) {
this._fx('css', element, css);
return this;
};
JqFxStack.prototype.show_block = function(element) {
this._fx('css', element, {'display': 'block'});
return this;
};
JqFxStack.prototype.show_inline = function(element) {
this._fx('css', element, {'display': 'inline'});
return this;
};
JqFxStack.prototype.hide = function(element) {
this._fx('css', element, {'display': 'none'});
return this;
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.