最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript setTimeout() Method on Animation - Stack Overflow

programmeradmin1浏览0评论

I have three yellow bars and each of them needs to e from left to right. For that, I have produced this code, but it only works on the last one. Can anyone correct this code; I need to work with pure JavaScript. I am not using any framework. Thanks.

window.onload = function(){  

    var yellowTitles = document.getElementById('magazine-brief').getElementsByTagName('h2');

    for(i=0; i< yellowTitles.length; i++) {
        var header = yellowTitles[i];
        var timer = i*500;
        var yellowBar = setTimeout(animeYellowBar,timer);

        function animeYellowBar(){
           header.style.left= "0";
        }

    }

}

I have three yellow bars and each of them needs to e from left to right. For that, I have produced this code, but it only works on the last one. Can anyone correct this code; I need to work with pure JavaScript. I am not using any framework. Thanks.

window.onload = function(){  

    var yellowTitles = document.getElementById('magazine-brief').getElementsByTagName('h2');

    for(i=0; i< yellowTitles.length; i++) {
        var header = yellowTitles[i];
        var timer = i*500;
        var yellowBar = setTimeout(animeYellowBar,timer);

        function animeYellowBar(){
           header.style.left= "0";
        }

    }

}
Share Improve this question edited Apr 30, 2019 at 8:47 AshNaz87 4324 silver badges15 bronze badges asked Feb 22, 2011 at 11:08 3gwebtrain3gwebtrain 15.3k29 gold badges141 silver badges276 bronze badges 3
  • 2 PLEASE use the code button in the toolbar to format the code. You're overriding my edit over and over again. – Robert Koritnik Commented Feb 22, 2011 at 11:14
  • yes. you are correct, can you help me how to place the code and information usually? – 3gwebtrain Commented Feb 22, 2011 at 11:30
  • Use toolbar to format your code. It will ident it by 4 spaces. Refer to this help about formatting: stackoverflow./editing-help – Robert Koritnik Commented Feb 22, 2011 at 11:34
Add a ment  | 

4 Answers 4

Reset to default 2

Here's how I'd solve the problem:

var yellows = document.getElementById('magazine-brief').getElementsByTagName('h2');

// this will force the header number to be bound correctly
// also animates the div across the page by tracking the current position of x
function createMotion(num){
  var currPos = 0;//current x position
  var delta = 10;//move by this amount
  setInterval(function(){
    currPos += delta
    yellows[num].style.left = currPos;
  }, num * 500);
}

for (var i = 1; i < yellows.length; i++)
{
  createMotion(i);
}

Note the function "createMotion" - added so the number "i" is correctly reference in the setInterval function.

Shouldn't you be incrementing your CSS left value instead of just setting it to 0? Why have a timeout at all if you're just going to set the value without gradually incrementing or decrementing?

If you do actually want to use a gradual animation, look at this tutorial : http://www.schillmania./content/projects/javascript-animation-1/

Very descriptive and possibly what you want.

By the time your timeout function runs, header refers to your last h2.

Try editing your timeout function to this:

function animeYellowBar(){
       var thisheader=header;
       thisheader.style.left= "0";
    }
var yellows = document.getElementById('magazine-brief').getElementsByTagName('h2');

for (var i = 0; i < yellows.length; i++)
{
    (function(idx, el){
        window.setTimeout(function(){
            var interval = window.setInterval(function(){
               el.style.left = parseInt(el.style.left) + 10; // adjust this movement step
               if (parseInt(el.style.left) >= 0)
               {
                   el.style.left = 0;
                   window.clearInterval(interval);
               }
            }, 100); // adjust this number for animation speed
        }, (idx++) * 500);
    })(i, yellows[i]);
}
发布评论

评论列表(0)

  1. 暂无评论