家里有个兔崽子 2018-03-12 01:05 采纳率: 50%
浏览 3617
已采纳

如何在video退出全屏按esc时用js执行操作?

我点击任意一个class为iframe的a按钮,会将alt的地址赋给video的src,然后全屏播放
现在我想做的是按esc退出视频全屏时将video的src地址清空

 <div class="vdobox">
    <video id="mario-video" controls autoplay="autoplay">
        <source src="">
    </video>
</div>
<a  alt="http://www.guangfan.com/Public/Gf2016/images/video2.mp4" class="iframe">video2</a>
<a  alt="http://www.guangfan.com/Public/Gf2016/images/video.mp4" class="iframe">video</a>
<h1 id="heool" align="center" style="color: #000;">hello</h1>

<script>
    var aLi = document.querySelectorAll('.iframe');
    for (var i = 0; i < aLi.length; i++) {
        aLi[i].addEventListener('click', function(){
            //开始视频弹出事件
            var marioVideo = document.getElementById("mario-video");
                    var hhh=document.getElementById("heool");
                    document.getElementById("heool").style.color="blue";
            if (marioVideo && this) {
                    marioVideo.src=this.getAttribute("alt");
                    if (marioVideo.requestFullscreen) {
                        marioVideo.requestFullscreen();
                    }
                    else if (marioVideo.msRequestFullscreen) {
                        marioVideo.msRequestFullscreen();
                    }
                    else if (marioVideo.mozRequestFullScreen) {
                        marioVideo.mozRequestFullScreen();
                    }
                    else if (marioVideo.webkitRequestFullScreen) {
                        marioVideo.webkitRequestFullScreen();
                    }
            }
            //结束
        }
        );
    }

    //网上找的退出全屏,不能在video全屏时执行下边的js事件
    $(function(){
        $(window).keyup(function (event) {
            if (event.keyCode == 27) {
                alert("执行退出全屏操作...");
                document.getElementById("heool").style.color="black";
            }
        });
    })



</script>
  • 写回答

2条回答 默认 最新

  • Go 旅城通票 2018-03-12 02:46
    关注

    全部了无法响应keyup事件,可以用计时器定时检查video的高度,但是你的video标签要设置一个默认高度,计时器检查如果高度变回一样了就执行代码,如下下面的

    
    <div class="vdobox">
        <video id="mario-video" controls autoplay="autoplay" height="500">
            <source src="">
        </video>
    </div>
    <a alt="http://www.guangfan.com/Public/Gf2016/images/video2.mp4" class="iframe">video2</a>
    <a alt="http://www.guangfan.com/Public/Gf2016/images/video.mp4" class="iframe">video</a>
    <h1 id="heool" align="center" style="color: #000;">hello</h1>
    
    <script>
        var aLi = document.querySelectorAll('.iframe');
        var  timer, videoHight = 500;///////////
        for (var i = 0; i < aLi.length; i++) {
            aLi[i].addEventListener('click', function () {
                //开始视频弹出事件
                var marioVideo = document.getElementById("mario-video");
                var hhh = document.getElementById("heool");
                document.getElementById("heool").style.color = "blue";
               ///////////////////////////
                timer = setInterval(function () {
                    if (marioVideo.offsetHeight == videoHight) {
                        alert("执行退出全屏操作...");
                        document.getElementById("heool").style.color = "black";
                        clearInterval(timer)
                    }
                }, 500)
                ///////////////////////////
                if (marioVideo && this) {
                    marioVideo.src = this.getAttribute("alt");
                    if (marioVideo.requestFullscreen) {
                        marioVideo.requestFullscreen();
                    }
                    else if (marioVideo.msRequestFullscreen) {
                        marioVideo.msRequestFullscreen();
                    }
                    else if (marioVideo.mozRequestFullScreen) {
                        marioVideo.mozRequestFullScreen();
                    }
                    else if (marioVideo.webkitRequestFullScreen) {
                        marioVideo.webkitRequestFullScreen();
                    }
                }
                //结束
            }
            );
        }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?