banner
DIYgod

Hi, DIYgod

写代码是热爱,写到世界充满爱!
github
twitter
follow
bilibili
telegram
email
steam
playstation
nintendo switch

I have hidden it.

titlehide

Witnessed a magical scene on Sonic 853, a kayak, decisively posted it to the water.

Original Author: Kurayama Mirai, who is unhappy all day

What the hell: Changing the title when leaving and entering the page

Principle:

Using the HTML5 Page Visibility API

Currently, the page visibility API has two properties and one event, as follows:

document.hidden: A boolean value indicating whether the current page is visible or not.

document.visibilityState: Returns the visibility state of the current page, with possible values of hidden, visible, prerender, and preview.

visibilitychange: An event triggered when the visibility state changes.

Previously, I only knew that I could detect page visibility through iframe + onblur/onfocus events. With this API, it's much more convenient and elegant.

Code:

var OriginTitile = document.title;
var titleTime;
document.addEventListener('visibilitychange', function() {
    if (document.hidden) {
        document.title = '(つェ⊂) I've hidden it~ ' + OriginTitile;
        clearTimeout(titleTime);
    }
    else {
        document.title = '(*´∇`*) You found it~ ' + OriginTitile;
        titleTime = setTimeout(function() {
            document.title = OriginTitile;
        }, 2000);
    }
});
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.