<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <div style="width:50px; height:50px; background-color:red;margin-left:2px;">
        文字
    </div>
</body>
</html>
<script>

    var px = 5;
    var myReq;
    function test() {
        var div = document.querySelector('div');

        div.style.marginLeft = (parseInt(div.style.marginLeft.replace('px', '')) + px) + 'px';

        //if (parseInt(div.style.marginLeft.replace('px', '')) > 500) {
        //    px = -px;
        //}

        //if (parseInt(div.style.marginLeft.replace('px', '')) <= 0) {
        //    px = -px;
        //}

        if (parseInt(div.style.marginLeft.replace('px', '')) > 500) {
            //window.cancelAnimationFrame(myReq);

            return;
        }

        myReq = window.requestAnimationFrame(test);
    }

    myReq = window.requestAnimationFrame(test);
</script>

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-14
猜你喜欢
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
  • 2021-08-14
  • 2021-12-09
  • 2021-11-16
  • 2021-09-14
相关资源
相似解决方案