Skip to content

Instantly share code, notes, and snippets.

@soofaloofa-zz
Created May 18, 2011 13:51
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 soofaloofa-zz/978601 to your computer and use it in GitHub Desktop.
Save soofaloofa-zz/978601 to your computer and use it in GitHub Desktop.
Dissecting jQuery's Fade Animation
function $(id) {
var $ = document.getElementById(id);
var opacityTo = function (elm, v) {
elm.style.opacity = v/100; // CSS3
elm.style.MozOpacity = v/100; // FF
elm.style.KhtmlOpacity = v/100; // Safari
elm.style.filter=" alpha(opacity ="+v+")"; // IE
};
$.fade = function (delay, callbk, out) {
var _this = this;
// for ie
_this.style.zoom = 1;
_this.style.display="block";
for (var i = 1; i <= 100; i++) {
(function(j) {
setTimeout(function() {
if (out == true) {
j = 100 - j;
}
opacityTo(_this, j);
if (j == 100 && callbk != undefined) {
callbk.call(_this);
}
else if (out == true && callbk != undefined && j == 0) {
callbk.call(_this);
}
}, j*delay/100);
})(i);
}
};
$.fadein : function(elm, delay, callbk) {
$.fade(elm, delay, callbk, false);
};
$.fadeout : function(elm, delay, callbk) {
$.fade(elm, delay, callbk, true);
};
return $;
})();
@soofaloofa-zz
Copy link
Author

Usage:
$("imgid").fadeIn(1000);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment