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);
}
});