网络浏览器上全屏模式视频的热点叠加

问题描述 投票:0回答:1

我编写了一些代码来显示视频叠加热点。但是,当我将视频全屏显示时,热点不再可见或不可单击。我尝试在全屏模式下检查元素,当我将指针拖动到其

上时,热点会突出显示(不可见或不可单击),但它们相对于视频的位置会错误。 在窗口模式下,这些都不是问题。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Interactive Video with Responsive Hotspots</title>
        <style>
            /* Responsive container for the video and hotspots */
            #videoContainer {
                position: relative;
                max-width: 100%;
                height: auto;
            }

            /* Video element should maintain aspect ratio */
            #myVideo {
                width: 100%;
                height: auto;
                display: block;
            }

           /* Hotspot styling */
           .hotspot {
                position: absolute;
                background-color: rgba(255, 0, 0, 0.5); /* Semi-transparent red */
                color: white;
                cursor: pointer;
                display: none; /* Hidden by default */
                transition: all 0.2s ease;
            }
        </style>
     </head>
    <body>
        <div id="videoContainer">
            <video id="myVideo" controls>
                <source src="video1.mp4" type="video/mp4">
                Your browser does not support the video tag.
            </video>
            <div id="hotspot1" class="hotspot" onclick="hotspotClick('Hotspot 1')">1</div>
            <div id="hotspot2" class="hotspot" onclick="hotspotClick('Hotspot 2')">2</div>
            <div id="hotspot3" class="hotspot" onclick="hotspotClick('Hotspot 3')">3</div>
            <div id="hotspot4" class="hotspot" onclick="hotspotClick('Hotspot 4')">4</div>
        </div>

        <script>
            const video = document.getElementById('myVideo');
            const hotspot1 = document.getElementById('hotspot1');
            const hotspot2 = document.getElementById('hotspot2');
            const hotspot3 = document.getElementById('hotspot3');
            const hotspot4 = document.getElementById('hotspot4');

            // Define the relative positions (as percentages) and rectangular padding for the hotspots
            const hotspotData = {
                 hotspot1: {
                    showTime: 3, // Show at 3 seconds
                    hideTime: 6, // Hide at 6 seconds
                    positions: { leftPercent: 18, topPercent: 30, widthPercent: 8, heightPercent: 4 }
                },
                 hotspot2: {
                    showTime: 3,
                    hideTime: 6,
                    positions: { leftPercent: 35, topPercent: 30, widthPercent: 8, heightPercent: 4 }
                },
                 hotspot3: {
                    showTime: 3,
                    hideTime: 6,
                    positions: { leftPercent: 52, topPercent: 30, widthPercent: 8, heightPercent: 4 }
                },
                 hotspot4: {
                    showTime: 3,
                    hideTime: 6,
                    positions: { leftPercent: 68, topPercent: 30, widthPercent: 8, heightPercent: 4 }
                }
            };

            // Adjust hotspot position and size based on video and container size, with rectangular          padding
            function adjustHotspotPosition(hotspot, video, positionData) {
                const videoRect = video.getBoundingClientRect();
                const left = (positionData.leftPercent / 100) * videoRect.width;
                const top = (positionData.topPercent / 100) * videoRect.height;
                const width = (positionData.widthPercent / 100) * videoRect.width;
                const height = (positionData.heightPercent / 100) * videoRect.height;

                hotspot.style.left = left + 'px';
                hotspot.style.top = top + 'px';
                hotspot.style.width = width + 'px';
                hotspot.style.height = height + 'px';
                hotspot.style.padding = (height * 0.2) + 'px ' + (width * 0.2) + 'px'; // Rectangular padding
            }

        // Toggle visibility of the hotspot based on video time and update its position
        function toggleHotspot(hotspot, data, currentTime) {
            if (currentTime >= data.showTime && currentTime <= data.hideTime) {
                hotspot.style.display = 'block';
                adjustHotspotPosition(hotspot, video, data.positions);
            } else {
                hotspot.style.display = 'none';
            }
        }

        // Event listener to update hotspots on video time update
        video.addEventListener('timeupdate', function () {
            const currentTime = video.currentTime;

            toggleHotspot(hotspot1, hotspotData.hotspot1, currentTime);
            toggleHotspot(hotspot2, hotspotData.hotspot2, currentTime);
            toggleHotspot(hotspot3, hotspotData.hotspot3, currentTime);
            toggleHotspot(hotspot4, hotspotData.hotspot4, currentTime);
        });

        // Event listener to adjust hotspot positions when the window resizes or video enters fullscreen
        function adjustAllHotspots() {
            adjustHotspotPosition(hotspot1, video, hotspotData.hotspot1.positions);
            adjustHotspotPosition(hotspot2, video, hotspotData.hotspot2.positions);
            adjustHotspotPosition(hotspot3, video, hotspotData.hotspot3.positions);
            adjustHotspotPosition(hotspot4, video, hotspotData.hotspot4.positions);
        }

        // Listen for window resize to reposition hotspots
        window.addEventListener('resize', adjustAllHotspots);

        // Listen for fullscreen change to reposition hotspots for all browsers (Chrome/Brave included)
        function addFullscreenListeners() {
            document.addEventListener('fullscreenchange', adjustAllHotspots);      // Standard
            document.addEventListener('webkitfullscreenchange', adjustAllHotspots); // Safari/Chrome
            document.addEventListener('mozfullscreenchange', adjustAllHotspots);    // Firefox
            document.addEventListener('MSFullscreenChange', adjustAllHotspots);     // IE/Edge
        }

        addFullscreenListeners();

        // Optional: Action on hotspot click
        function hotspotClick(hotspotName) {
            alert(hotspotName + ' clicked!');
        }
    </script>
</body>
</html>

我只需要嵌入在 html5 浏览器中的热点视频,根据我希望它们出现的时间戳来保持其形状、大小和位置。我的代码可以在窗口模式下运行,但不能在任何浏览器上以全屏模式运行

当您单击 <video> 标签上的 全屏

 按钮时,文档本身将保持在 窗口化 状态。在全屏模式下,只有视频内容会覆盖其他所有内容。因此,这会导致您的热点无法显示(因为它们被视频覆盖)。

为了实现你想要的,你可以拦截 全屏 按钮,该按钮应该执行以下操作(只是建议):

  1. 隐藏您的视频。
  2. 创建一个覆盖屏幕的包装器,其
    z-index
    比覆盖的元素更高。
  3. 通过javascript创建动态图像。
  4. 对图像进行动画处理以显示
    <video>
    的当前帧。
  5. 将生成的图像应用到包装器的背景。
  6. 在正确的时间将您的热点放入包装纸内。
  7. 在叠加层添加视频控件。

然后,当用户离开 全屏模式时:

  1. 取消隐藏您的视频。
  2. 隐藏您的叠加层。
javascript css html5-video overlay
1个回答
0
投票

当您单击 <video> 标签上的 全屏

 按钮时,文档本身将保持在 窗口化 状态。在全屏模式下,只有视频内容会覆盖其他所有内容。因此,这会导致您的热点无法显示(因为它们被视频覆盖)。

为了实现你想要的,你可以拦截 全屏 按钮,该按钮应该执行以下操作(只是建议):

  1. 隐藏您的视频。
  2. 创建一个覆盖屏幕的包装器,其
    z-index
    比覆盖的元素更高。
  3. 通过javascript创建动态图像。
  4. 对图像进行动画处理以显示
    <video>
    的当前帧。
  5. 将生成的图像应用到包装器的背景。
  6. 在正确的时间将您的热点放入包装纸内。
  7. 在叠加层添加视频控件。

然后,当用户离开 全屏模式时:

  1. 取消隐藏您的视频。
  2. 隐藏您的叠加层。
© www.soinside.com 2019 - 2024. All rights reserved.