JS监听浏览器TAB被激活或者被离开

JS监听浏览器TAB被激活或者被离开

监听窗口被离开或者被选中可以使用以下方法来实现

document.addEventListener('visibilitychange', function () {
    // 用户息屏、或者切到后台运行 (离开页面)
    if (document.visibilityState === 'hidden') {
        console.log('hidden')
    }
    // 用户打开或回到页面
    if (document.visibilityState === 'visible') {
        console.log('页面可见')
    }

 

实例

https://elearning.sjdc.net.cn:30000/#/student/my-course

https://elearning.sjdc.net.cn:30000/static/js/chunk-elementUI.e3431999.js

直接搜索visibilitychange

 visibilityChangeHandler: function() {
                        var e = this
                          , t = document.visibilityState;
                        "hidden" === t ? this.focusable = !1 : "visible" === t && setTimeout((function() {
                            e.focusable = !0
                        }
                        ), 50)
                    },
                        
                   mounted: function() {
                    var e = this;
                    Object(Ji["addResizeListener"])(this.$el, this.update),
                    document.addEventListener("visibilitychange", this.visibilityChangeHandler),
                    window.addEventListener("blur", this.windowBlurHandler),
                    window.addEventListener("focus", this.windowFocusHandler),
                    setTimeout((function() {
                        e.scrollToActiveTab()
                    }
                    ), 0)
                },
                beforeDestroy: function() {
                    this.$el && this.update && Object(Ji["removeResizeListener"])(this.$el, this.update),
                    document.removeEventListener("visibilitychange", this.visibilityChangeHandler),
                    window.removeEventListener("blur", this.windowBlurHandler),
                    window.removeEventListener("focus", this.windowFocusHandler)
                }

 

也可以直接在Chrome上查看

 

https://elearning.sjdc.net.cn:30000/static/js/chunk-commons.facf42be.js

mounted: function() {
                var e = this;
                setTimeout((function() {
                    e.bindTimeUpdateEvent()
                }
                ), 1e3)
            },
            methods: {
                bindTimeUpdateEvent: function() {
                    var e = this;
                    e.timeList = [],
                    e.courseResourceLibraryInfo && Object(s["q"])(e.courseResourceLibraryInfo.FastVideoName) && (document.getElementById("videoComponent").ontimeupdate = function() {
                        e.timeList.push(this.currentTime),
                        e.currentTime = this.currentTime,
                        e.duration = this.duration,
                        e.$emit("timeupdateVideo", {
                            timeList: e.timeList
                        }),
                        this.ended && (e.timeList = [])
                    }
                    )
                },

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(1950)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-11-20 11 JavaScript Utility Libraries you Should Know in 2019
2019-11-20 The Five Qualities You Need in a Partner
2018-11-20 DataContractAttribute.IsReference
2014-11-20 sqlite的Top筛选
点击右上角即可分享
微信分享提示