I'm trying to create a simple animation using setInterval in JavaScript. The goal is to make an image move from left to right.
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>animation</title>
<style type="text/css">
#image
{
position: relative;
left: 1px;
}
</style>
<script type="text/javascript" src="animation.js"></script>
</head>
<body>
<img src="smile.jpg" id="image" alt="Smile" width="100" height="100" />
</body>
</html>
JavaScript:
window.onload = initialize;
function initialize()
{
setInterval(function(){animate();},1000);
}
function animate()
{
var obj = document.getElementById("image");
var value = parseInt(obj.style.left, 10);
obj.style.left = (value + 10) + "px";
}
Unfortunately, the JavaScript code isn't working as expected and I'm not sure why. The animate function is executing but the image's position is not changing.