前端实现画中画,让网页飞出浏览器

前端实现画中画,让网页飞出浏览器

在前端开发中,我们常常追求创新的交互方式来提升用户体验。今天,我要和大家分享一个非常酷的功能:文档画中画(Document Picture-in-Picture,简称 PiP)。这个功能可以让你网页上的任何内容悬浮在桌面上,无论用户如何切换页面,它都能始终保持在屏幕的最上层。

一、PiP 的核心实现

PiP 的核心方法是 window.documentPictureInPicture.requestWindow,这是一个异步方法,返回一个新创建的 window 对象。这个对象可以看作是一个新的网页,但它始终悬浮在屏幕上方。

xml 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document Picture-in-Picture API 示例</title>
    <style>
        #pipContent {
            width: 600px;
            height: 300px;
            background: pink;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div id="container">
        <div id="pipContent">这是一个将要放入画中画的 div 元素!</div>
        <button id="clickBtn">切换画中画</button>
    </div>
    <script>
        // 检查是否支持 PiP 功能
        if ('documentPictureInPicture' in window) {
            console.log("🚀 浏览器支持 PiP 功能!");
        } else {
            console.warn("⚠️ 当前浏览器不支持 PiP 功能,更新浏览器或者换台电脑吧!");
        }

        // 请求 PiP 窗口
        document.getElementById("clickBtn").addEventListener("click", async function () {
            const pipContent = document.getElementById("pipContent");

            // 请求创建一个 PiP 窗口
            const pipWindow = await window.documentPictureInPicture.requestWindow({
                width: 200,  // 设置窗口的宽度
                height: 300  // 设置窗口的高度
            });

            // 将原始元素克隆并添加到 PiP 窗口中
            pipWindow.document.body.appendChild(pipContent.cloneNode(true));

            // 设置 PiP 样式同步
            [...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.addEventListener("pagehide", (event) => {
                console.log("已退出 PIP 窗口");
            });

            pipWindow.addEventListener('focus', () => {
                console.log("PiP 窗口进入了焦点状态");
            });

            pipWindow.addEventListener('blur', () => {
                console.log("PiP 窗口失去了焦点");
            });
        });
    </script>
</body>
</html>

二、设置 PiP 样式

为了让 PiP 窗口更加美观,我们需要同步原始页面的样式。可以通过 document.styleSheets 获取所有样式信息,并将其应用到 PiP 窗口中。

ini 复制代码
[...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 事件

我们还可以为 PiP 窗口添加事件监听,监控画中画模式的进入和退出,以及焦点变化。

javascript 复制代码
// 监听退出 PiP 模式的事件
pipWindow.addEventListener("pagehide", (event) => {
    console.log("已退出 PIP 窗口");
});

// 监听焦点和失焦事件
pipWindow.addEventListener('focus', () => {
    console.log("PiP 窗口进入了焦点状态");
});

pipWindow.addEventListener('blur', () => {
    console.log("PiP 窗口失去了焦点");
});

四、完整示例

通过以上步骤,我们已经实现了一个完整的 PiP 功能。点击按钮后,指定的元素将悬浮在屏幕上方,并且可以随用户操作进行相应的反馈。

xml 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document Picture-in-Picture API 示例</title>
    <style>
        #pipContent {
            width: 600px;
            height: 300px;
            background: pink;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div id="container">
        <div id="pipContent">这是一个将要放入画中画的 div 元素!</div>
        <button id="clickBtn">切换画中画</button>
    </div>

    <script>
        // 检查是否支持 PiP 功能
        if ('documentPictureInPicture' in window) {
            console.log("🚀 浏览器支持 PiP 功能!");
        } else {
            console.warn("⚠️ 当前浏览器不支持 PiP 功能,更新浏览器或者换台电脑吧!");
        }

        // 请求 PiP 窗口
        document.getElementById("clickBtn").addEventListener("click", async function () {
            const pipContent = document.getElementById("pipContent");

            // 请求创建一个 PiP 窗口
            const pipWindow = await window.documentPictureInPicture.requestWindow({
                width: 200,  // 设置窗口的宽度
                height: 300  // 设置窗口的高度
            });

            // 将原始元素克隆并添加到 PiP 窗口中
            pipWindow.document.body.appendChild(pipContent.cloneNode(true));

            // 设置 PiP 样式同步
            [...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.addEventListener("pagehide", (event) => {
                console.log("已退出 PIP 窗口");
            });

            pipWindow.addEventListener('focus', () => {
                console.log("PiP 窗口进入了焦点状态");
            });

            pipWindow.addEventListener('blur', () => {
                console.log("PiP 窗口失去了焦点");
            });
        });
    </script>
</body>
</html>

希望这篇文章能帮助你掌握 Document Picture-in-Picture API 的使用,为你的项目带来更灵活的交互体验。

相关推荐
用户47949283569154 分钟前
你每天都在用的 JSON.stringify ,V8 给它开了“加速通道”
前端·chrome·后端
JIngJaneIL10 分钟前
基于java+ vue办公管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
静待雨落21 分钟前
Electron无边框窗口如何拖拽以及最大化和还原窗口
前端·electron
沐泽__1 小时前
iframe内嵌页面双向通信
前端·javascript·chrome
小北方城市网1 小时前
第4 课:Vue 3 路由与状态管理实战 —— 从单页面到多页面应用
前端·javascript·vue.js
ohyeah1 小时前
用 Vue3 + Coze API 打造冰球运动员 AI 生成器:从图片上传到风格化输出
前端·vue.js·coze
Dragon Wu1 小时前
TailWindCss 核心功能总结
前端·css·前端框架·postcss
SHolmes18542 小时前
给定某日的上班时间段,计算当日的工作时间总时长(Python)
开发语言·前端·python
掘金安东尼2 小时前
顶层元素问题:popover vs. dialog
前端·javascript·面试
掘金安东尼2 小时前
React 的新时代已经到来:你需要知道的一切
前端·javascript·面试