html - 如何在 React js 中点击按钮全屏播放 HTML5 视频

标签 html reactjs react-hooks

我试图在点击外部按钮时全屏显示视频。我尝试使用 ref 和 document.getElementByid() 。它为 requestFullscreen() 抛出错误。是否有任何其他方法可以实现此目的。

  const fullScreenVideoRef=useRef(null);
  const  onShowVideo=()=>{
  if (fullScreenVideoRef.current.requestFullscreen) {
            marioVideo.current.requestFullscreen();
        }
        else if (fullScreenVideoRef.current.msRequestFullscreen) {
            marioVideo.msRequestFullscreen();
        }
        else if (fullScreenVideoRef.current.mozRequestFullScreen) {
            marioVideo.current.mozRequestFullScreen();
        }
        else if (fullScreenVideoRef.current.webkitRequestFullScreen) {
            marioVideo.current.webkitRequestFullScreen();
        }
   }
  <video muted autoPlay loop controls id="full-screenVideo" ref={fullScreenVideoRef} >
                        <source src="/video/web-experience-cdn.mp4" type="video/mp4"/>
  </video>

最佳答案

像这样使用:

toggleFullScreen = () => {
    var el = document.getElementById("full-screenVideo");
    if (el.requestFullscreen) {
      el.requestFullscreen();
    } else if (el.msRequestFullscreen) {
      el.msRequestFullscreen();
    } else if (el.mozRequestFullScreen) {
      el.mozRequestFullScreen();
    } else if (el.webkitRequestFullscreen) {
      el.webkitRequestFullscreen();
    }
  };

...

<video muted autoPlay loop controls id="full-screenVideo" ref={fullScreenVideoRef} >
                    <source src="/video/web-experience-cdn.mp4" type="video/mp4"/>
</video>

样本:HERE

示例代码:HERE

关于html - 如何在 React js 中点击按钮全屏播放 HTML5 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59854885/

相关文章:

reactjs - usePagination 自定义 Hook 与 material-ui 使用分页组件中的 next 和 prev 函数作为 Prop

html - 如何在滚动条中水平显示图像?

css - 带有 :href and a link over it all 的 div

javascript - 在 ReactJS 中渲染 HTMLDivElements

javascript - 如何在 React 的 JSX 中编写 for 循环?

reactjs - React、Typescript、Hooks - 类型上不存在属性 'state'

reactjs - 了解 React 钩子(Hook) useEffect 函数。useEffect 中的回调函数(即取消订阅)何时被调用?

javascript - 查询字符串中的重复变量

javascript - 如何在 Canvas 中获取图像元素的坐标

JavaScript index.js .babelrc 错误 "Do not clone comments tha...<omitted>... } could not be cloned"