Skip to content

Instantly share code, notes, and snippets.

@garystorey
Created January 5, 2015 21:53
Show Gist options
  • Save garystorey/60b332c54430821e06e6 to your computer and use it in GitHub Desktop.
Save garystorey/60b332c54430821e06e6 to your computer and use it in GitHub Desktop.
fadeIn.js
Element.prototype.fadeIn = function(callback) {
var ele = this,
opacity = ele.style.opacity = ele.style.opacity || 0.0,
fadeInLoop = null,
intervalTime = 10,
opacityAmount = 0.05;
ele.style.display = 'block';
// If the opacity is already greater than or equal to zero then there is nothing
// for us to do and we break out here.
if (opacity >= 1.0) {
return false;
}
// Loop through until we set the opacity to 1.0
fadeInLoop = setInterval(function() {
opacity += opacityAmount;
ele.style.opacity = opacity;
ele.style.filter = 'alpha(opacity=' + opacity * 100 + ')';
if (opacity >= 1.0) {
clearInterval(fadeInLoop);
if (typeof callback !== "undefined") {
callback();
}
}
}, intervalTime);
}
/* Example
document.getElementById('some-id').fadeIn(function() {
console.log('Faded in the element.');
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment