簡體   English   中英

顯示滾動消息-不起作用

[英]Display scrolling message - doesn't work

我是JavaScript的初學者,並根據此Workshop:顯示滾動消息嘗試做一些簡單的核心示例。

但是,此變體滾動消息不起作用。 而且我不明白為什么會這樣。 我的網絡瀏覽器Google Chrome瀏覽Google Chrome ,編碼為ANSI

碼:

<html>
    <head><title>Scrolling Message Example</title>
        <script>
            msg = "This is an example of a scrolling message. Isn't it exciting?";
            msg = "...          ..." + msg;
            pos = 0;

            function ScrollMessage() {
               window.status = msg.substring(pos, msg.length) + msg.substring(0, pos);
               pos++;
               if (pos > msg.length) pos = 0;
               window.setTimeout("ScrollMessage()", 200);
            }

            ScrollMessage();
    </script>
        </head>
        <body>
            <h1>Scrolling Message Example</h1>
            Look at the status line at the bottom of this page. (Don't
            watch it too long, as it may be hypnotic.)
        </body>
</html>

題:

  • 如何解決這個麻煩?
  • 還有哪些其他變體更適合用於此目的?

請參閱有關JavaScript window.status的帖子。

出於安全考慮,大多數瀏覽器默認情況下默認禁用window.status。

我認為練習是關於滾動效果的,而不是關於狀態欄的。

如果您更換

window.status = msg.substring(pos, msg.length) + msg.substring(0, pos);

console.log(msg.substring(pos, msg.length) + msg.substring(0, pos));

您會在控制台中看到創建滾動效果的函數本身運行正常。

如果要顯示滾動消息,只需在要顯示消息的頁面中創建一個<div> ,並在每次調用滾動功能時更新div的內容。

我修改了您的示例:

<html>
    <head><title>Scrolling Message Example</title>
        <script>
            var msg = "This is an example of a scrolling message. Isn't it exciting?";
            msg = "...          ..." + msg;
            var pos = 0;

            function ScrollMessage() {
                document.getElementById("scrollMsgContainer").innerHTML = msg.substring(pos, msg.length) + msg.substring(0, pos);
                pos++;
                if (pos > msg.length) pos = 0;
                window.setTimeout(ScrollMessage, 200);
            }
        </script>
    </head>
    <body>
        <h1>Scrolling Message Example</h1>
        Look at the status line at the bottom of this page. (Don't
        watch it too long, as it may be hypnotic.)

        <div id="scrollMsgContainer" style="position: fixed; bottom: 0; width: 100%; background-color: #DDDDDD">&nbsp;</div>
    </body>
    <script>
        ScrollMessage();
    </script>
</html>

為什么不使用document.title代替:

function ScrollMessage() {
               document.title = msg.substring(pos, msg.length) + msg.substring(0, pos);
               pos++;
               if (pos > msg.length) pos = 0;
               window.setTimeout("ScrollMessage()", 200);
            }

除IE <= 7外,所有瀏覽器均支持該功能。

檢查MDN window.status參考。 Google Chrome沒有狀態欄,而Mozilla Firefox默認阻止狀態欄更改。

您可以嘗試使用HTML中的“ 粘性頁腳”並設置其內容。

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM