页面部分/完全隐藏

时间:2013-09-06 15:34:02

标签: html5

我需要查看页面(浏览器标签)是否被部分/完全隐藏。 页面可见性api允许我找到paritally visible / full visible,完全隐藏。

我需要一些方法来查找页面是否被部分/完全隐藏。

http://www.w3.org/TR/page-visibility/#sec-document-interface

2 个答案:

答案 0 :(得分:0)

此代码可能有效

(function() {
    var hidden = "hidden";

    // Standards:
    if (hidden in document)
        document.addEventListener("visibilitychange", onchange);
    else if ((hidden = "mozHidden") in document)
        document.addEventListener("mozvisibilitychange", onchange);
    else if ((hidden = "webkitHidden") in document)
        document.addEventListener("webkitvisibilitychange", onchange);
    else if ((hidden = "msHidden") in document)
        document.addEventListener("msvisibilitychange", onchange);
    // IE 9 and lower:
    else if ('onfocusin' in document)
        document.onfocusin = document.onfocusout = onchange;
    // All others:
    else
        window.onpageshow = window.onpagehide 
            = window.onfocus = window.onblur = onchange;

    function onchange (evt) {
        var v = 'visible', h = 'hidden',
            evtMap = { 
                focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h 
            };

        evt = evt || window.event;
        if (evt.type in evtMap)
            document.body.id = evtMap[evt.type];
        else        
            document.body.id = this[hidden] ? "hidden" : "visible";
    }
})();

答案 1 :(得分:0)

function getHiddenProp(){
    var prefixes = ['webkit','moz','ms','o'];

    // if 'hidden' is natively supported just return it
    if ('hidden' in document) return 'hidden';

    // otherwise loop over all the known prefixes until we find one
    for (var i = 0; i < prefixes.length; i++){
        if ((prefixes[i] + 'Hidden') in document) 
            return prefixes[i] + 'Hidden';
    }

    // otherwise it's not supported
    return null;
}

有关详细信息,请参阅this链接