// Get the elements
var element = document.getElementById('target');
// Run the function
animate(element, {width: 10, height: 10}, 1000, function() {
console.log('Done...');
});
// Create the function
function animate(element, properties, timeframe, callback) {
// Set a default timeframe
if(timeframe === undefined) timeframe = 800;
alert(properties.length);
// Loop through all properties
for(var p in properties) {
// Make sure the style is set
element.style[p] = (window.getComputedStyle) ?
window.getComputedStyle(element, null)[p] :
element.currentStyle[p];
// Get the original
var original = parseInt(element.style[p]);
// Work out the difference
var difference = properties[p] - original;
// Work out how many frames
var frames = timeframe / (1000 / 60);
// Work out how many pixels per frame
var pixels = difference / frames;
// Loop through each frame
for(var i = 0; i <= frames; i++)
{
setTimeout((function(exti, element) {
return function() {
element.style[p] = original + (pixels * exti) + 'px';
}
})(i, element), i * (1000 / 60), element);
}
// Correct floating point problem
/*setTimeout((function(privateEye, elements) {
return function() {
elements[e].style.width = width + 'px';
elements[e].style.height = height + 'px';
}})(i, this.elements), timeframe, this.elements);*/
}
// Set callback timer
if(callback !== undefined)
setTimeout(callback, timeframe);
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.