离开页面动态网页标题JS

😂 这篇文章最后更新于1501天前,您需要注意相关的内容是否还可用。

当用户离开含此JS页面窗口时触发事件更改标题内容

<script>;(function () {
    var defaultTitle = document.title;
    var isRollTitle = 0; //为1时滚动标题
    var rollTimeDelay = '500'; //滚动间隔
    var step = 0;
    var titles = ["牛逼","武汉加油!","爱我"]; //随机语
    var showStyle = 'random';
    // 滚动标题
    if (isRollTitle) setInterval(function() {
        document.title = document.title.substring(1, document.title.length) + document.title.substring(0, 1);
    }, rollTimeDelay);

    // 离开页面
    document.addEventListener('visibilitychange', function () {
        if (document.visibilityState == 'hidden') {
            switch (showStyle) {
                case "random":
                    step = parseInt(Math.random() * titles.length);
                    break;
                default:
                case "order":
                    if (step >= titles.length) step = 0;
                    break;
            }
            document.title = titles[step];
            step++;
        } else {
            document.title = defaultTitle;
        }
    });})();</script>