Created
January 5, 2015 21:53
-
-
Save garystorey/60b332c54430821e06e6 to your computer and use it in GitHub Desktop.
fadeIn.js
This file contains hidden or 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
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