13

如何创建像facebook一样的闪烁标题效果?意思是,当您与某人聊天并收到新消息时,标题开始在原始标题和通知用户新消息到达的消息之间切换,并具有闪烁效果。

AdrianoKF的解释:

请注意,在收到新的聊天消息后,窗口标题在“来自 Foo Bar 的新消息”和常规消息之间循环。

4

3 回答 3

37

代码:

(function () {

var original = document.title;
var timeout;

window.flashTitle = function (newMsg, howManyTimes) {
    function step() {
        document.title = (document.title == original) ? newMsg : original;

        if (--howManyTimes > 0) {
            timeout = setTimeout(step, 1000);
        };
    };

    howManyTimes = parseInt(howManyTimes);

    if (isNaN(howManyTimes)) {
        howManyTimes = 5;
    };

    cancelFlashTitle(timeout);
    step();
};

window.cancelFlashTitle = function () {
    clearTimeout(timeout);
    document.title = original;
};

}());

用法:

flashTitle("New Message from Matt Lunn");

... 或者...

flashTitle("New Message from John Smith", 10); // toggles it 10 times.
于 2010-08-01T09:46:44.350 回答
12

设置每隔几秒切换一次标题的间隔。未经测试的代码:

function flashTitle(pageTitle, newMessageTitle)
{
    if (document.title == pageTitle)
    {
        document.title = newMessageTitle;
    }
    else
    {
        document.title = pageTitle;
    }
}

setInterval("flashTitle('Facebook', 'New message from John Doe!')", 800);
于 2010-08-01T09:37:00.483 回答
1
setInterval("var x='www.WHAK.com/Packer/',y='WHAK.com/Packer/',z=document;z.title=z.title==x?y:x",900);
于 2015-06-15T08:45:32.217 回答