文字滚动功能无法运行

时间:2018-09-13 17:10:43

标签: javascript

这是我的代码:

<!doctype html>
<html>
  <head>
    <title>Algorithms</title>
    <script type="text/javascript">
    function move_paragraph() {
      next = current + "px";
      current + -1;
      if (current > 300) {
        current = 0;
      }
      paragraph.style.left = next;
      var rate = 18;
      setTimeout(move_paragraph, rate);
    }
    function init() {
      paragraph = document.getElementById("original");
      paragraph.style.position = "absolute";
      current = 0;
      move_paragraph();
    }
    </script>
    <link href="style.css" rel="stylesheet" type="text/css">
  </head>
  <body class="Algbody" onload="init();">
    <p id="original">This is a text scroll</p>
    <br>
    <br>
  </body>
</html>

它应该是基本的文本滚动,但不会在IE,Firefox或Chrome中运行。谁能看到一个错误?

2 个答案:

答案 0 :(得分:1)

第8行有-而不是=。

current +- 1;更改为current += 1;

答案 1 :(得分:-1)

您可以使用CSS而不是Javascript来实现。

<!doctype html>
<html>
<head>
    <title>Algorithms</title>
    <link href="style.css" rel="stylesheet" type="text/css">
    <style>
        @keyframes text-scroll {
            0% { left: 0; }
            100% { left: 300px; }
        }

        #original {
            position: absolute;
            animation: text-scroll 3s forwards;
        }
    </style>
</head>

<body class="Algbody">
    <p id="original">This is a text scroll</p>
    <br>
    <br>
</body>
</html>