html实现iframe全屏

前言

html浏览器全屏操作,基于jquery

iframe全屏、指定标签全屏

实现

css

css 复制代码
/** 全屏*/
.lay-dbclick-box{
	position: relative;
	width: 100%;
    height: 100%;
}
.lay-dbclick-screen{
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
    height: 100%;
    z-index: 99999999999999;
}
.lay-fullScreen{
	position: fixed;
    left: 0;
    top: 0;
    border-radius: 0;
    padding: 0;
    margin: 0;
    width: 100%;
    height: 100%;
    z-index: 9999999999999;
}

html

关键是lay-dbclick-box和lay-dbclick-screen,其中的iframe是内容

html 复制代码
<div class="lay-dbclick-box">
	<iframe src="" width="100%" height="100%" id="fullb" frameborder="0" allowfullscreen="" ></iframe>
	<div class="lay-dbclick-screen"></div>
</div>

js

javascript 复制代码
openFullScreen();

/**
 * -------------------------------------------------------------------------------------------------------
 * 通用全屏操作
 */
function openFullScreen(){
	// 双击全屏/退出全屏
	$(".lay-dbclick-screen").dblclick(function(){
		var val = $(this).parent().attr("lay-svalue");
		if(!val){
			$(this).parent().addClass("lay-fullScreen");
			$(this).parent().attr("lay-svalue", 1);
			fullScreen();
		}else{
			$(this).parent().removeClass("lay-fullScreen");
			$(this).parent().attr("lay-svalue", "");
			exitFullscreen();
		}
	})
	// 全屏事件监听
	document.addEventListener("fullscreenchange", function(){
        if (getFullscreenElement() == null) {
        	console.log("-----------------[退出全屏]--------------")
           $(".lay-fullScreen").attr("lay-svalue", "");
			$(".lay-fullScreen").removeClass("lay-fullScreen");
			exitFullscreen();
        }else {
            console.log("-----------------[打开全屏]--------------")
        }
    })
}

/**
 * -------------------------------------------------------------------------------------------------------
 * 获取全屏状态
 */
function getFullscreenElement(){
    return (
        document['fullscreenElement'] ||
        document['mozFullScreenElement'] ||
        document['msFullScreenElement'] ||
        document['webkitFullscreenElement'] || null
    );
}

/**
 * -------------------------------------------------------------------------------------------------------
 * 全屏
 */
function fullScreen() {
    var element = document.documentElement;
    if (element.requestFullscreen) {
        element.requestFullscreen();
    } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen();
    } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
    } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen();
    }
}

/**
 * --------------------------------------------------------------------------------------------------------
 * 退出全屏
 */
function exitFullscreen() {
    if (document.exitFullscreen) {
        document.exitFullscreen();
    } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
    }
}
相关推荐
墨菲安全32 分钟前
NPM组件 betsson 等窃取主机敏感信息
前端·npm·node.js·软件供应链安全·主机信息窃取·npm组件投毒
GISer_Jing33 分钟前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript
天涯学馆33 分钟前
前端开发也能用 WebAssembly?这些场景超实用!
前端·javascript·面试
我在北京coding2 小时前
TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
前端·javascript·vue.js
Eiceblue2 小时前
使用 C# 发送电子邮件(支持普通文本、HTML 和附件)
开发语言·c#·html·visual studio
前端开发与ui设计的老司机2 小时前
UI前端与数字孪生结合实践探索:智慧物流的货物追踪与配送优化
前端·ui
全能打工人2 小时前
前端查询条件加密传输方案(SM2加解密)
前端·sm2前端加密
海天胜景2 小时前
vue3 获取选中的el-table行数据
javascript·vue.js·elementui
翻滚吧键盘3 小时前
vue绑定一个返回对象的计算属性
前端·javascript·vue.js
苦夏木禾3 小时前
js请求避免缓存的三种方式
开发语言·javascript·缓存