图像序列动画只加载一次不是无限的

时间:2018-01-25 07:09:02

标签: javascript html css

下面是我的代码,在这个1到15的图像中,此图像逐个加载到15,并在特定时间间隔内从1到15再次开始。在这个序列中,当页面加载时图像无限播放,但我想在页面加载时只播放一次图像。 我无法修复此错误,请帮我解决这个问题。



onload = function startAnimation() { 
  var frames = document.getElementById("animation").children;
  var frameCount = frames.length;
  var i = 0;
  setInterval(function () { 
      frames[i % frameCount].style.display = "none";
      frames[++i % frameCount].style.display = "block";
  }, 100); 
}

#animation img {
    display: none;
}
#animation img:first-child {
    display: block;
}

<div id="animation"> 
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging01.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging02.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging03.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging04.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging05.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging06.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging07.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging08.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging09.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging10.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging11.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging12.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging13.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging14.png">
  <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging15.png">
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您应该使用clearInterval();

&#13;
&#13;
onload = function startAnimation() { 
    var frames = document.getElementById("animation").children;
    var frameCount = frames.length;
    var i = 0;
    var myInterval=setInterval(function () { 
       if(i==frameCount-2){
         clearInterval(myInterval);
        }
        frames[i % frameCount].style.display = "none";
        frames[++i % frameCount].style.display = "block";
        
    }, 100); 
}
&#13;
#animation img {
    display: none;
}
#animation img:first-child {
    display: block;
}
&#13;
<div id="animation"> 
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging01.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging02.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging03.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging04.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging05.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging06.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging07.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging08.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging09.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging10.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging11.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging12.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging13.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging14.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging15.png" />
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

@pratik,这应该对你有所帮助: 您应该使用clearInterval,如下所示:

if(j === 15) {
  clearInterval(interval);
}

实施例

&#13;
&#13;
onload = function startAnimation() {
            var animDiv = document.getElementById('animation');
            var x = document.createElement("IMG");
            x.setAttribute("id", 'test');
            x.setAttribute("src",
                "http://jumpingfishes.com/dancingpurpleunicorns/charging01.png");
            animDiv.appendChild(x);

            var i = 1,
                j = 2;
            var interval = setInterval(function () {
                var y = (i < 10) ? '0' + i.toString() : i.toString(),
                    z = (j < 10) ? '0' + j.toString() : j.toString(),
                    url = document.getElementById('test').src;
                document.getElementById('test').src = url.replace(y, z);
                i++;
                j++;
                if (j === 16) {
                    clearInterval(interval);
                }
            }, 100);
        }
&#13;
#animation img {
  display: none;
}

#animation img:first-child {
  display: block;
}
&#13;
<div id="animation">

</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

一旦您希望它停止运行,您必须清除间隔。使用clearInterval()

检查此示例:

请注意,intervalEndi到达时的数字,将清除您的间隔。

&#13;
&#13;
onload = function startAnimation() { 
    var frames = document.getElementById("animation").children;
    var frameCount = frames.length;
    var i = 0;
    var intervalEnd = 100;
    var intervalId = setInterval(function () { 
        frames[i % frameCount].style.display = "none";
        frames[++i % frameCount].style.display = "block";
        if (i >= intervalEnd)
          clearInterval(intervalId)
    }, 100); 
}
&#13;
#animation img {
    display: none;
}
#animation img:first-child {
    display: block;
}
&#13;
<div id="animation"> 
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging01.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging02.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging03.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging04.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging05.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging06.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging07.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging08.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging09.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging10.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging11.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging12.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging13.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging14.png" />
    <img src="http://jumpingfishes.com/dancingpurpleunicorns/charging15.png" />
</div>
&#13;
&#13;
&#13;