js点击弹窗播放h5视频,点击关闭窗口暂停播放

2023-08-28
3,519次阅读
没有评论

我们使用document添加了一个点击事件监听器,来判断点击事件的目标元素是否位于弹窗内部(videoPopup)以及是否不是 “打开弹窗” 按钮。只有当点击事件的目标元素不在弹窗内部且不是 “打开弹窗” 按钮时,才会触发关闭弹窗的逻辑。

这样,当你点击video-popup-container区域以外的任何地方时,弹窗都会关闭。

截图

js点击弹窗播放h5视频,点击关闭窗口暂停播放

演示代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>H5视频弹窗示例</title>
    <style>
        img{
            max-width: 100%;
        }
        button{
            border: none;
            background: none;
            cursor: pointer;
        }
        #open-popup{
            padding: 10px 20px;
            background: #000;
            color: #FFF;
            border-radius: 5px;
        }
        #video-popup-container {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5); /* 半透明的灰色背景 */
            justify-content: center;
            align-items: center;
            z-index: 1000; /* 确保遮罩位于其他内容之上 */
        }

        #video-popup {
            width: 800px;
            position: relative;
        }
        #close-popup {
            position: absolute;
            top: -50px;
            right: -30px;
            width: 50px;
        }
        #close-popup:hover img{
            opacity: .8;
        }
    </style>
</head>

<body>
    <button id="open-popup">打开弹窗</button>
    <div id="video-popup-container">
        <div id="video-popup">
            <video id="video" controls width="100%" height="100%">
                <source src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/byted-player-videos/1.0.0/xgplayer-demo-720p.mp4" type="video/mp4">
                Your browser does not support the video tag.
            </video>
            <button id="close-popup"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAACXBIWXMAAAsTAAALEwEAmpwYAAAFJGlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjO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XFLlFu1JSFaydpqL1gXexQYnXn+IKiLY/I+WV0Wna6auX9rosQLS70J+OnE8MKJbdwVGBaJ/APcBo2aAyd7PpK1ZBTZqmVrNhZQ0mnzgRoxBtG5mnK6PT7lTW0vP0ZV3ApLKAZ4zjxzDt+5SeoFgmPqFMPOTlkhe7TVnbi3VjWe3Lup/q31WEao7qw5SbJG173VbgjDaIxa6TfuCEYvxhmgcD4CvgbsX44TpBLIDsATYEjn0WmDeImUufA7cHjg3tc7xMFkCuDRw3AxwxiJdbX1NNR/+l7+s4twDyScCY53DeuaTUGeCeLj8/DXxQy7PRKmS+y4rjcIJVUC7rF5EfO+r9TESuyr3KgmqL/3Tb1xeBh4B3rAI41Tqq/zJ0HfATug8t+JcsgawBJqlWXcvAx1QrkyKFmtIfcsXI8+73K1IFiDMVIM5UgDhTAeJMBYgzFSDOVIA4UwHiTAWIMxUgzlSAONM/ijKN1AJ6VeAAAAAASUVORK5CYII=" alt=""></button>
        </div>
    </div>

    <script>
        document.addEventListener("DOMContentLoaded", function () {
                const openPopupButton = document.getElementById("open-popup");
                const closePopupButton = document.getElementById("close-popup");
                const videoPopupContainer = document.getElementById("video-popup-container");
                const videoPopup = document.getElementById("video-popup");
                const video = document.getElementById("video");

                openPopupButton.addEventListener("click", function () {
                    videoPopupContainer.style.display = "flex";
                    video.play();
                });

                closePopupButton.addEventListener("click", function () {
                    closePopup();
                });

                document.addEventListener("click", function (event) {
                    if (!videoPopup.contains(event.target) && event.target !== openPopupButton) {
                        closePopup();
                    }
                });

                function closePopup() {
                    videoPopupContainer.style.display = "none";
                    video.pause();
                }
            });
    </script>
</body>
</html>

正文完
要饭中,多少给点吧(支付宝)
post-qrcode
 13
评论(没有评论)
验证码