javascript 如何在标题标签中放置滚动文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16354122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:18:35  来源:igfitidea点击:

How to put scrolling text in title tag?

javascriptjqueryhtml

提问by Yadav Chetan

I want to make text of title scrollable, I make the code as under it scrolls fine but the text I entered is displayed without space, meaning space in string is not considered.

我想让标题文本可滚动,我使代码在它下面滚动得很好,但我输入的文本显示时没有空格,这意味着不考虑字符串中的空格。

<script type="text/javascript">
    //function for tittle scrolling
    function scrlsts() {
        //var scrltest = " Nature ";
        scrltest=document.title;

        //alert(scrltest);
        scrltest = scrltest.substring(1, scrltest.length) + scrltest.substring(0, 1);

        //alert(scrltest);
        document.title = scrltest;

        setTimeout("scrlsts()", 1000);
    }
    $(document).ready(function() {
        var scrltest = " Nature dff ssfd ";
        document.title=scrltest;
        scrlsts();
    });
</script> 

Thanks in advance

提前致谢

回答by Joseph

Haven't made these for a long time, but this should work:

很久没有做这些了,但这应该有效

(function titleScroller(text) {
    document.title = text;
    setTimeout(function () {
        titleScroller(text.substr(1) + text.substr(0, 1));
    }, 500);
}(" Nature dff ssfd "));

回答by Cameron

I made an easy and simple JavaScript libraryto accomplish this task.

我制作了一个简单的JavaScript 库来完成这个任务。

回答by Gregory R.

<script type='text/javascript'> 
title = "Your Title";
position = 0;
function scrolltitle() {
    document.title = title.substring(position, title.length) + title.substring(0, position); 
    position++;
    if (position > title.length) position = 0;
    titleScroll = window.setTimeout(scrolltitle,170);
}
scrolltitle();
</script>

To stop the title scroll, just run:

要停止标题滚动,只需运行:

window.clearTimeout(titleScroll);

回答by Madhuri

You can try this :

你可以试试这个:

<script>

    var repeat=0 //enter 0 to not repeat scrolling after 1 run, othersise, enter 1
    var title=document.title
    var leng=title.length
    var start=1
    function titlemove() 
    {
        titl=title.substring(start, leng) + title.substring(0, start)
        document.title=titl
        start++
        if (start==leng+1`enter code here`) 
        {
            start=0
            if (repeat==0)
            return
        }
        setTimeout("titlemove()",500)
    }
    if (document.title)
    titlemove()
</script>