Skip to content

Instantly share code, notes, and snippets.

@chwkai
Created January 30, 2010 09:24
Show Gist options
  • Select an option

  • Save chwkai/290490 to your computer and use it in GitHub Desktop.

Select an option

Save chwkai/290490 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Animation demo</title>
<style type="text/css">
#content{
height:200px;
width:200px;
background:red;
}
</style>
<script type="text/javascript">
var Samples = {};
/**
*
* @param {Object} element "element to perform the animation"
* @param {Object} duration "the duration of the animation"
* @param {Object} fps
* @param {Object} easing "function for caculating the transition"
* @param {Object} callback "function to call when animation completed"
*/
Samples.show = function(element, duration, fps, easing, callback){
var interval = 1000 / fps;
element.style.opacity = 0;
var offsets = easing(element.style.opacity, 1, duration, fps);
var intervalId = window.setInterval(_onTick, interval);
function _onTick(){
if(offsets.length > 0){
var offsetValue = offsets.shift();
element.style.opacity = parseFloat(element.style.opacity) + offsetValue;
}
else{
window.clearInterval(intervalId);
if(callback){
callback.apply(element, null);
}
}
}
};
Samples.linear = function(currentValue, targetValue, duration, fps){
var offsets = [];
var times = duration * fps / 1000;
var offsetValue = (targetValue - currentValue) / times;
for(var i = 0; i < times; i++){
offsets.push(offsetValue);
}
return offsets;
};
window.onload = function(){
document.getElementById("show").onclick = function(){
Samples.show(document.getElementById("content"), 1000, 40, Samples.linear, onFinished);
}
};
function onFinished(){
window.alert("animation has finished");
}
</script>
</head>
<body>
<div><input type="button" id="show" value="show me"/></div>
<div id="content"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment