浏览器画中画?还有谁!!!

逻辑追梦说
• 阅读 104

浏览器画中画?还有谁!!!

作者GitHub:https://github.com/gitboyzcf 有兴趣可关注!!!

效果

浏览器画中画?还有谁!!!

尽管效果可以,但是api兼容性不理想🤦‍

步入正题

安全上下文:此功能仅在安全上下文(HTTPS)中可用,在某些或所有支持的浏览器中。

Document Picture-in-Picture API(文档画中画API)

文档画中画API(pip api)可以打开一个始终在顶部的窗口,该窗口可以填充任意HTML内容,它扩展了早期的 <video> 画中画API,专门支持将HTML <video> 元素放入始终在顶部的窗口中。

兼容性

浏览器画中画?还有谁!!!

window 内置 pip API

  • documentPictureInPicture对象
    它通过 window.documentPictureInPicture属性访问。
    实例属性

    • window返回一个 Window 实例,表示画中画窗口内的浏览器上下文

    实例方法

    • requestWindow()打开当前主浏览上下文的“画中画”窗口。返回一个 Promise ,返回结果就是一个“画中画”窗口Window实例。
    • close()关闭当前画中画窗口

    事件

    • enter当画中画窗口成功打开时激发。
    • unload当画中画窗口关闭时触发。
    • click点击画中画文档时触发。

    注意点(仔细阅读理解)

  • 网站一次只能打开一个画中画窗口,多个按钮触发画中画会进行替换。
  • 无法导航画中画窗口(更改为新文档的任何 window.history 或 window.location 调用将关闭PiP窗口)。
  • 画中画窗口永远不会超过打开的窗口。这意味着任何将打开器更改为新文档的导航(即使是同源导航)都将导致PiP窗口关闭。
  • 画中画窗口将浮在其他窗口的顶部。
  • 画中画窗口生命周期只存在于当前标签页。

演示代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>画中画</title>
  <style>
    body {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-flow: column;
      gap: 15px;
    }

    #main-content {
      padding: 15px;
      border: 1px solid red;
    }

    .svg-icon {
      fill: var(--color);
      width: 48px;
      height: 48px;
      transition: fill 300ms;
    }

    .loader {
      position: relative;
      width: 150px;
      height: 150px;
      background: transparent;
      border-radius: 50%;
      box-shadow: 25px 25px 75px rgba(0, 0, 0, 0.55);
      border: 1px solid #333;
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
    }

    .loader::before {
      content: '';
      position: absolute;
      inset: 20px;
      background: transparent;
      border: 1px dashed#444;
      border-radius: 50%;
      box-shadow: inset -5px -5px 25px rgba(0, 0, 0, 0.25),
        inset 5px 5px 35px rgba(0, 0, 0, 0.25);
    }

    .loader::after {
      content: '';
      position: absolute;
      width: 50px;
      height: 50px;
      border-radius: 50%;
      border: 1px dashed#444;
      box-shadow: inset -5px -5px 25px rgba(0, 0, 0, 0.25),
        inset 5px 5px 35px rgba(0, 0, 0, 0.25);
    }

    .loader span {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 50%;
      height: 100%;
      background: transparent;
      transform-origin: top left;
      animation: radar81 2s linear infinite;
      border-top: 1px dashed #fff;
    }

    .loader span::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: seagreen;
      transform-origin: top left;
      transform: rotate(-55deg);
      filter: blur(30px) drop-shadow(20px 20px 20px seagreen);
    }

    @keyframes radar81 {
      0% {
        transform: rotate(0deg);
      }

      100% {
        transform: rotate(360deg);
      }
    }
  </style>
</head>

<body>
  <button id="popupbtn" title="Toggle PIP Mode">
    <svg class="svg-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48">
      <path
        d="M38 14H22v12h16V14zm4-8H6c-2.21 0-4 1.79-4 4v28c0 2.21 1.79 3.96 4 3.96h36c2.21 0 4-1.76 4-3.96V10c0-2.21-1.79-4-4-4zm0 32.03H6V9.97h36v28.06z" />
    </svg>
  </button>
  <div class="main">
    <div id="main-content">
      <div class="loader">
        <span></span>
      </div>
    </div>
  </div>

  <script>
    const isPip = () => "documentPictureInPicture" in window
    const pipActuator = (targetDomId, popupBtnDomId) => {
      if (!isPip()) {
        alert("您的浏览器不支持画中画模式!");
        return;
      }
      let targetContainer = null;
      let pipWindow = null;
      let pipTip = null;

      async function enterPiP() {
        const target = document.querySelector(targetDomId);
        targetContainer = target.parentNode;

        pipTip = document.createElement("div");
        pipTip.classList.add("pip-tip");
        pipTip.innerHTML = "画中画模式已开启";
        targetContainer.appendChild(pipTip);

        const pipOptions = {
          initialAspectRatio: target.clientWidth / target.clientHeight,
          lockAspectRatio: true,
          copyStyleSheets: true,
        };

        pipWindow = await documentPictureInPicture.requestWindow(pipOptions);

        // 从初始文档复制样式表
        // 使画中画里面的画面和初始文档保持一致
        [...document.styleSheets].forEach((styleSheet) => {
          try {
            const cssRules = [...styleSheet.cssRules].map((rule) => rule.cssText).join("");
            const style = document.createElement("style");

            style.textContent = cssRules;
            pipWindow.document.head.appendChild(style);
          } catch (e) {
            const link = document.createElement("link");

            link.rel = "stylesheet";
            link.type = styleSheet.type;
            link.media = styleSheet.media;
            link.href = styleSheet.href;
            pipWindow.document.head.appendChild(link);
          }
        });

        // 将目标添加到PiP窗口
        pipWindow.document.body.append(target);

        // 监听pip点击事件
        pipWindow.addEventListener('click', () => {
          console.log('点击了画中画文档');
        });
        // 监听PiP结束事件,将目标放回原位
        pipWindow.addEventListener("unload", onLeavePiP.bind(pipWindow), {
          once: true,
        });
      }

      // 当PiP窗口关闭时调用
      function onLeavePiP() {
        if (this !== pipWindow) {
          return;
        }

        // 将目标添加回目标窗口
        const target = pipWindow.document.querySelector(targetDomId);
        targetContainer.append(target);
        targetContainer.removeChild(pipTip)
        pipWindow.close();

        pipWindow = null;
        targetContainer = null;
      }
      document.querySelector(popupBtnDomId).addEventListener("click", () => {
        if (!pipWindow) {
          enterPiP();
        } else {
          onLeavePiP.bind(pipWindow)();
        }
      });
    }
    pipActuator("#main-content", "#popupbtn");
  </script>
</body>

</html>

到这里就结束了,后续还会更新 前端 系列相关,还请持续关注!
感谢阅读,若有错误可以在下方评论区留言哦!!!

浏览器画中画?还有谁!!!

推荐文章👇

Document Picture-in-Picture Specification
Tomodoro

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
3年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这