zoukankan      html  css  js  c++  java
  • 浏览器的visibilitychange 事件ie10以下不兼容

    方法1,

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script type="text/javascript" src="http://mat1.gtimg.com/www/js/jquery/jquery-1.11.1.min.js"></script>
        <script>
    /***
       切换浏览器tab,判断当前tab是否活跃
    ***/
        (function(g, h, $, b) {
            var e, i, f = 'onfocusin' in h && 'hasFocus' in h ? 'focusin focusout' : 'focus blur',
                d = ['', 'moz', 'ms', 'o', 'webkit'],
                c = $.support,
                a = $.event;
            while ((i = e = d.pop()) != b) {
                i = (e ? e + 'H' : 'h') + 'idden';
                if (c.pageVisibility = typeof h[i] == 'boolean') {
                    f = e + 'visibilitychange';
                    break
                }
            }
            $(/blur$/.test(f) ? g : h).bind(f,
                function(m) {
                    var l = m.type,
                        j = m.originalEvent,
                        k = j.toElement;
                    if (!/^focus./.test(l) || (k == b && j.fromElement == b && j.relatedTarget == b)) {
                        a.trigger((i && h[i] || /^(?:blur|focusout)$/.test(l) ? 'hide' : 'show') + '.visibility')
                    }
                })
        }(this, document, jQuery));
    
        var o={}
        o.$tabFlag = true;
         /**  切换页卡 **/  
        var changeTab=function() {
            console.log(333)
            $(document).bind({
                'show.visibility': function() { // 当前活跃
                    o.$tabFlag = true;
                    console.log(222)
                },
                'hide.visibility': function() { // 失去当前状态
                    o.$tabFlag = false;
                    console.log(11111)
                }
            });
        }
        
        changeTab();
        </script>
    </head>
    <body>    
    </body>
    </html>

     方法2

                    var o={};
                    // 找到当前浏览器支持的hidden属性名和visibilitychange事件名
                    var hidden, visibilityChange;
                    if (typeof document.hidden !== "undefined") {
                        hidden = "hidden";
                        visibilityChange = "visibilitychange";
                    } else if (typeof document.mozHidden !== "undefined") {
                        hidden = "mozHidden";
                        visibilityChange = "mozvisibilitychange";
                    } else if (typeof document.msHidden !== "undefined") {
                        hidden = "msHidden";
                        visibilityChange = "msvisibilitychange";
                    } else if (typeof document.webkitHidden !== "undefined") {
                        hidden = "webkitHidden";
                        visibilityChange = "webkitvisibilitychange";
                    }
                    $(document).bind(visibilityChange,function(){
                        if (document[hidden]) {
                            console.log(1111)
                            o.$tabFlag = false;
                            // videoElement.pause();
                        } else {
                            console.log(222)
                            o.$tabFlag = true;
                            // videoElement.play();
                        }
                    })
    FeatureChrome (Webkit)Firefox (Gecko)Internet ExplorerOperaSafari (WebKit)
    Basic support 13 webkit 10 (10) moz
    18 (18)
    10 ms 12.10[*] Not supported
    cssfirefly http://cssfirefly.cnblogs.com
  • 相关阅读:
    Unity 3(一):简介与示例
    MongoDB以Windows Service运行
    动态SQL中变量赋值
    网站发布IIS后堆栈追踪无法获取出错的行号
    GridView Postback后出错Operation is not valid due to the current state of the object.
    Visual Studio 2010 SP1 在线安装后,找到缓存在本地的临时文件以便下次离线安装
    SQL Server 问题之 排序规则(collation)冲突
    IIS 问题集锦
    linux下安装mysql(ubuntu0.16.04.1)
    apt-get update 系列作用
  • 原文地址:https://www.cnblogs.com/cssfirefly/p/3982100.html
Copyright © 2011-2022 走看看