Created
March 7, 2013 20:17
-
-
Save alanpich/5111448 to your computer and use it in GitHub Desktop.
jQuery plugin to that allows styles to be set temporarily to an element, and will revert to their previous state after an allotted time or when a specified event is triggered on the element
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
/** | |
* jQuery plugin that allows styles to be set temporarily | |
* to an element, and will revert to their previous state | |
* after an allotted time or when a specified event is triggered | |
* | |
* @author Alan Pich <alan.pich@gmail.com> | |
* | |
* @param {Object} styles | |
* @param {String|Number} timeOrEvent | |
*/ | |
$.fn.temporaryCss = function(styles, timeOrEvent){ | |
// Loop through each indivudual element in turn so we can create a closure | |
for(var k=0; k < this.length; k++){ | |
// Closure for remembering any current inline styles | |
(function(elem,styles,timeOrEvent){ | |
$elem = $(elem); | |
// Save previous inline styles | |
previousInlineStyles = {} | |
for( i in styles ){ | |
previousInlineStyles[i] = elem.style[i]; | |
} | |
// Add the new styles | |
$elem.css(styles); | |
// Is this an event or a timeout? | |
if(isNaN(parseInt(timeOrEvent))){ | |
// String - bind event listener | |
$elem.one(timeOrEvent,function(){ | |
$(this).css(previousInlineStyles); | |
}) | |
} else { | |
// Integer - set timeout | |
setTimeout(function(){return function(){ | |
$elem.css(previousInlineStyles); | |
}}($elem),timeOrEvent); | |
} | |
})(this[k],styles,timeOrEvent); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment