Skip to content

Latest commit

 

History

History
43 lines (41 loc) · 1.36 KB

监听窗口是否是活动窗口.md

File metadata and controls

43 lines (41 loc) · 1.36 KB

监听当前窗口显示隐藏的切换

(function (vue) {
      var hidden = "hidden";
      // Standards:
      if (hidden in document) {
        document.addEventListener("visibilitychange", onchange);
        console.log('visibilitychange');
      }
      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)
          console.log(1);
        else
          vue.getList()
      }
      if (document[hidden] !== undefined)
        onchange({ type: document[hidden] ? "blur" : "focus" });
    })(this);