2Truth
2Truth

Reputation: 65

Animation of a gif reverse to another gf

I have an animated gif of a cat running from left to right, and then turning around and running from right to left. It seems like everything in my code is right. Before I over complicated my code but it seemed to work perfect, however the gif was frozen when moving. Now I have revised my code, but now the gif doesn't reverse to the other gif. It just runs from left to right and then runs backwards. I can't seem to find any errors in my code. Can someone help.

<html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<style type="text/css">
#container {
    background:url(catBack1200.jpg) no-repeat;
    width:1200px;
    height:440px;
}
#catbox {
    position:absolute;
    top:330px;
    left:10px;
}
</style>
<script type="text/javascript">
  var switchDirection = false;
  function doAnimation() {
  var catbox = document.getElementById("catbox");
  var catimg = document.getElementById("cat");
  var currentLeft = catbox.offsetLeft;
  var newLocation;

  if (switchDirection == false)
  {
      newLocation = currentLeft + 3;
      catimg.src == "ani_cat.gif";

      if (currentLeft >= 600)
      {
          switchDirection = true;
      }
  }
  else
  {
      newLocation = currentLeft - 3;
       catimg.src == "ani_cat_rev.gif";

      if (currentLeft <= 0)
      {
          switchDirection = false;
      }
  }

  catbox.style.left = newLocation + "px";
  }
</script>
</head>

<body onload="setInterval(doAnimation, 10)"> 
<div id="container">
<div id="catbox">
    <img src="ani_cat.gif" id="cat" width="100" height="60" alt="busy kitty" />
</div>
</div>
</body>
</html>

Upvotes: 0

Views: 408

Answers (2)

gp.
gp.

Reputation: 8225

when setting image, use '=' instead of '==' I guess that would be typo.

Don't set src on every animation. rather set it when switching:

if (switchDirection == false) {
    newLocation = currentLeft + 3;

    if (currentLeft >= 600) {
        catimg.src = "ani_cat_rev.gif";
        switchDirection = true;
    }
} else {
    newLocation = currentLeft - 3;

    if (currentLeft <= 0) {
        catimg.src = "ani_cat.gif";
        switchDirection = false;
    }
}

Upvotes: 2

ohmygirl
ohmygirl

Reputation: 239

catimg.src == "ani_cat.gif";
catimg.src == "ani_cat_rev.gif";

Here is maybe the problem: you used the equal operator == instead of =.

Upvotes: 0

Related Questions