You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在window.onblur触发时将页面标题设置为动态滚动的自定义内容?

整合实现代码

你可以直接使用下面整合后的jQuery代码,已经将滚动标题效果和标签页聚焦/失焦逻辑结合,失焦时自动启动标题滚动,切回标签页时立即停止滚动并恢复原始标题:

jQuery(document).ready(function($) {
    // 存储原始页面标题
    var originalTitle = document.title;
    // 自定义失焦时滚动的标题内容
    var scrollTitleText = "Come back please ❤ Miss you ❤ Hello? ❤ ";
    // 存储滚动定时器ID,用于后续停止滚动
    var scrollTimer = null;

    // 标题滚动函数
    function titleScroller(text) {
        document.title = text;
        scrollTimer = setTimeout(function () {
            titleScroller(text.substr(1) + text.substr(0, 1));
        }, 200);
    }

    // 标签页失焦触发
    window.onblur = function () {
        // 启动滚动效果
        titleScroller(scrollTitleText);
    };

    // 标签页聚焦触发
    window.onfocus = function () {
        // 清除滚动定时器,停止滚动
        clearTimeout(scrollTimer);
        // 恢复原始标题
        document.title = originalTitle;
    };
});
逻辑说明
  • 新增了scrollTimer变量存储定时器ID,避免失焦时启动的滚动逻辑在切回页面后继续执行
  • 滚动的速度可以通过修改setTimeout的第二个参数(单位毫秒)调整,数值越小滚动速度越快
  • 你可以自行修改scrollTitleText的内容自定义滚动展示的文字

内容的提问来源于stack exchange,提问作者newbiemrt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 08:48:01