Changing the position of element with Javascript

前端 未结 2 1869
生来不讨喜
生来不讨喜 2021-01-22 05:12

This is supposed to change the position of a paragraph (with the id \'Text\') more to the right each time it is looped. It doesn\'t work though and I can\'t figure out how to fi

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-22 05:55

    try this one

    d have not any value so define d with

    d = document.getElementById('Text');

    and call your function myLoop so its work

    var x = 0;
    d = document.getElementById('Text');
    d.style.position = "absolute";
    myLoop();
    
    function myLoop() {
      setTimeout(function() {
        x += 10;
        d.style.left = x + 'px';
        myLoop();
      }, 100)
    }

提交回复
热议问题