简体   繁体   English

用javascript动画动态创建的div

[英]Animating dynamically created divs with javascript

I am trying to animate a dynamically created div with javascript, the id for the div is assigned when it is created, everything works fine until I attempt to animate one of the divs, I am trying this : 我正在尝试使用javascript为动态创建的div设置动画,在创建div时会为其分配id,一切正常,直到尝试为一个div设置动画,我正在尝试这样做:

function start() // Called from a button click
{

  var moveDiv= document.getElementById('Id0'); // Id0 is the Id of the div to move
  animate(moveDiv); // Recursive animate
}

function animate(inDiv) 
{
  inDiv.style.left = parseInt(inDiv.style.left)+1+'px';
  setTimeout(animate,20); // Recursive call
}

I know this is supposed to move the div infinitely to the left. 我知道这应该将div无限向左移动。 However nothing happens at all and I cannot figure out why, I don't think its the fact that I dynamically create the divs as I have checked all the Id's and they all exist so I don't think its because it can't find Id0, but just incase here is a snippet of my div creation code : 但是什么也没有发生,我也不知道为什么,我不认为这是我在检查所有Id且它们都存在时动态创建div的事实,所以我不认为它是因为找不到它Id0,但以防万一,这是我的div创建代码的片段:

for(var i=0; i<ca.length; i++)
      {
          var c = ca[i].trim();
          var start = c.indexOf('"coursename":"') + 14;
          var end = c.indexOf('","coursemark":"');
          var CC = c.substring(start,end);

          var start = c.indexOf('","coursemark":"') + 16;
          var end = c.indexOf('%"')+1;
          var CM = c.substring(start,end);
          var idCount = i;
          var div = document.createElement("div");
          div.style.width = "180px";
          div.style.height = "75px";
          div.style.backgroundColor = "rgba(0,100,175,0.8)";
          div.style.color = "white";
          div.style.marginTop = "2%";
          div.style.marginLeft = "50%";
          div.id = "sortID"+idCount;
          div.innerHTML = "Course Name : " + CC +  " Course Mark : " + CM + " Id : " + div.id;
          document.body.appendChild(div);

      }

This code works fine however and creates the divs perfectly, I just can't get any div to move. 这段代码可以正常工作,并且完美地创建了div,我只是无法移动任何div。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Couple of problems... 几个问题...

function start() // Called from a button click
{
  var moveDiv= document.getElementById('Id0'); // Id0 is the Id of the div to move
  animate(moveDiv); // Recursive animate
}
  • There is no element with ID of Id0 . 没有ID为Id0元素。 All of your generated element IDs look like sortID... 您生成的所有元素ID都类似于sortID...

And then... 接着...

function animate(inDiv) 
{
  inDiv.style.left = parseInt(inDiv.style.left)+1+'px';
  setTimeout(animate,20); // Recursive call
}
  • inDiv.style.left has never been initiated inDiv.style.left从未启动
  • You're not passing inDiv through to your recursive animate call 您没有将inDiv传递给递归animate调用

So firstly check your element references. 因此,首先检查您的元素引用。 Then make sure you're setting the position of the div correctly, or handling scenarios where it isn't yet set. 然后,请确保您正确设置了div的位置,或者在未设置div的情况下进行处理。 And finally make sure you pass inDiv through recursively. 最后确保您递归地通过inDiv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM