screenfull在线

image.png

  • .request
  • .exit
  • .toggle
  • .on
  • .off
  • .onchange
  • .onerror
  • .isFullscreen
  • .element
  • .isEnabled
  • .raw ```jsx npm install screenfull

import screenfull from ‘screenfull’;

// 判断浏览器是否允许全屏 if (screenfull.isEnabled) { screenfull.request(); }

// 移除 on() 监听事件 screenfull.off(‘change’, callback);

  1. vue 全屏 [https://blog.csdn.net/weixin_43926335/article/details/96309037](https://blog.csdn.net/weixin_43926335/article/details/96309037)
  2. <a name="eUXgl"></a>
  3. ## 新页面打开全屏
  4. 创建一个 iframe来显示新页面
  5. ```jsx
  6. import screenfull from 'screenfull';
  7. document.querySelector('#new-page-button').addEventListener(() => {
  8. const iframe = document.createElement('iframe')
  9. iframe.setAttribute('id', 'external-iframe');
  10. iframe.setAttribute('src', 'https://new-page-website.com');
  11. iframe.setAttribute('frameborder', 'no');
  12. iframe.style.position = 'absolute';
  13. iframe.style.top = '0';
  14. iframe.style.right = '0';
  15. iframe.style.bottom = '0';
  16. iframe.style.left = '0';
  17. iframe.style.width = '100%';
  18. iframe.style.height = '100%';
  19. document.body.prepend(iframe);
  20. document.body.style.overflow = 'hidden';
  21. });

cdn

https://cdnjs.com/libraries/screenfull.js/5.1.0
image.png

js 全屏 API

  • Element.requestFullscreen() 元素进入全屏模式
    • Element 通常是 HTML 节点,即 document.documentElement
  • document.exitFullscreen() 退出全屏
  • document.fullscreenElement 当前是否有节点处于全屏状态
  • document.fullscreenEnabled 是否支持全屏模式
  • document.fullscreenchange 进入全屏/离开全屏,触发 change 事件
  • document.fullscreenerror 无法进入全屏时触发

进入全屏

element.requestFullscreen();
document.documentElement.requestFullscreen();

  1. function requestFullscreen(element) {
  2. // w3c API
  3. if (element.requestFullscreen) {
  4. element.requestFullscreen();
  5. } else if (element.mozRequestFullScreen) {
  6. // firefox
  7. element.mozRequestFullScreen();
  8. } else if (element.webkitRequestFullScreen) {
  9. // webkit内核,例如 chrome
  10. element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
  11. } else if(element.msRequestFullscreen) {
  12. element.msRequestFullscreen()
  13. }
  14. }
  15. const fullscreenEnabled = document.fullscreenEnabled
  16. || document.mozFullScreenEnabled
  17. || document.documentElement.webkitRequestFullScreen;
  18. if (fullscreenEnabled) {
  19. // 是否有节点处于全屏状态
  20. if (document.fullscreenElement) return
  21. requestFullscreen(document.documentElement);
  22. }

退出全屏

document.exitFullscreen()

  1. if(document.exitFullscreen) {
  2. document.exitFullscreen()
  3. } else if (document.mozCancelFullscreen) {
  4. document.mozCancelFullscreen()
  5. } else if(document.webkitCancelFullscreen) {
  6. document.webkitCancelFullscreen()
  7. } else if ( document.msExitFullscreen) {
  8. document.msExitFullscreen()
  9. }

浏览器全屏参考
https://blog.csdn.net/weixin_50789156/article/details/126089849