【问题标题】:i have tried to pause video in html when user switch browser tab and when come back on that tab video play again automatically from where it stopped我试图在用户切换浏览器选项卡时暂停 html 中的视频,当返回该选项卡时,视频会从停止的位置自动重新播放
【发布时间】:2021-11-03 07:28:03
【问题描述】:

这是我在 html 文档中使用的简单视频标签脚本

var video = document.getElementById('video'); var documentTitle = 文档.title; var updateTitleForVideo = 函数(状态){ 如果(状态 === '可见') { 文档.title=文档标题; 返回; } document.title = documentTitle + '['+ state +']'; }; video.onpause = function() { updateTitleForVideo('暂停'); }; video.onplay = function() { updateTitleForVideo('播放'); }; document.addEventListener('visibilitychange' , function(){ var state = document.visibilityState; 如果(!video.paused){ 如果(状态 === '隐藏'){ 视频.暂停(); updateTitleForVideo('暂停'); } } 如果(!video.played){ 如果(状态 === '可见'){ 视频.play(); updateTitleForVideo('播放'); } } });

【问题讨论】:

    标签: javascript html sass


    【解决方案1】:
    const video  = document.getElementById("video")
    //The rest of your code
    // video.onpause etc...
    
    window.addEventListener("blur", function() {
    video.pause();
    })
    
    window.addEventListener("focus", function(){
    video.play();
    })
    

    more information

    【讨论】:

      猜你喜欢
      • 2015-03-17
      • 2016-06-29
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 2019-06-25
      相关资源
      最近更新 更多