0

我有一个猫从左到右跑的动画 gif,然后转身从右到左跑。似乎我的代码中的所有内容都是正确的。在我过度复杂我的代码但它似乎工作完美之前,但是 gif 在移动时被冻结了。现在我已经修改了我的代码,但现在 gif 并没有反转到另一个 gif。它只是从左到右运行,然后向后运行。我似乎在我的代码中找不到任何错误。有人可以帮忙吗。

<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>
4

2 回答 2

2

设置图像时,使用 '=' 而不是 '==' 我想那会是错字。

不要在每个动画上都设置 src。而是在切换时设置它:

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;
    }
}
于 2013-10-13T14:54:42.380 回答
0
catimg.src == "ani_cat.gif";
catimg.src == "ani_cat_rev.gif";

这可能是问题所在:您使用了等号运算符==而不是=.

于 2013-10-13T14:46:39.610 回答