jQuery在html页面标题标签中滚动字幕

sad*_*ave 2 tags jquery scroll marquee title

我想使用jquery在我的html title标签中放置一个滚动字幕,但不知道怎样也无法在任何地方在线找到一个好的解释.有谁可以帮助我吗?

Tat*_*nen 11

如果你只是想让它像marquee标签那样滚动,那就不是很难了:

(function titleMarquee() {
    document.title = document.title.substring(1)+document.title.substring(0,1);
    setTimeout(titleMarquee, 200);
})();
Run Code Online (Sandbox Code Playgroud)

这是非常基本的,但应该让你知道如何根据自己的喜好调整它.

  • 谢谢你的代码..除了一个故障,它运作良好.在第一次滚动之后,由于某种原因,所有空格都被移除,并且这些单词彼此相邻.我怎样才能保留空间?谢谢,菲尔 (2认同)

sed*_*ran 5

在塔图·乌尔曼嫩(Tatu Ulmanen)的回答中,空格字符存在问题。正如psarid所说,在第一次滚动之后,所有空格都将被删除。

这是因为html解析器会修剪文本。这意味着它将删除文本末尾和开头的空格。当标题滚动时,html中的标题对象如下所示:

<title>Scrolling Title With Spaces</title>
<title>crolling Title With SpacesS</title>
<title>rolling Title With SpacesSc</title>
<title>olling Title With SpacesScr</title>
...
<title>Title With SpacesScrolling</title>
Run Code Online (Sandbox Code Playgroud)

如您在上方看到的,我们失去了单词Scrolling和之间的空格Spaces。为防止这种情况,我们需要将原始document.title代码存储在javascript代码中的某个位置,并在其末尾添加一个空格或其他内容。然后,我们可以document.title通过滚动另一个变量中的文本来滚动。这是Tatu Ulmanen的修改后的代码。

var documentTitle = document.title + " - ";

(function titleMarquee() {
    document.title = documentTitle = documentTitle.substring(1) + documentTitle.substring(0,1);
    setTimeout(titleMarquee, 200);
})();
Run Code Online (Sandbox Code Playgroud)