问题:页面中使用 iframe 嵌入 PDF.js 的 viewer.html 时,部分浏览器全屏功能错误;
问题详情:问题出现的浏览器主要有:edge(win10 自带),火狐(按钮被屏蔽);
解决方案:
1. 不使用 iframe,新标签页打开;- 放弃:页面体验不好;
2. 不使用 iframe,使用模板布局,嵌套页面;- 放弃:模板与 PDF.js 样式冲突较多,适配后高耦合,不灵活;
3. 继续使用 iframe,保持原平台风格,重写 PDF.js 的全屏按钮操作;- 最终方案:快捷有效,低耦合;
全屏代码:
<a href="#" onClick="fullscreenAction()">button</a>
<p id="state" style="display:none;">no</p>
// 动作
function fullscreenAction() {var state = document.getElementById('state').innerText;
if (state == 'no') {fullscreen();
} else {exitFullscreen();
}
}
// 全屏
function fullscreen() {
var docElm = document.documentElement;
if (docElm.requestFullscreen) {docElm.requestFullscreen();
} else if (docElm.mozRequestFullScreen) {docElm.mozRequestFullScreen();
} else if (docElm.webkitRequestFullScreen) {docElm.webkitRequestFullScreen();
} else if (docElm.msRequestFullscreen) {docElm.msRequestFullscreen();
}
}
// 退出全屏
function exitFullscreen() {if (document.exitFullscreen) {document.exitFullscreen();
} else if (document.mozCancelFullScreen) {document.mozCancelFullScreen();
} else if (document.webkitCancelFullScreen) {document.webkitCancelFullScreen();
} else if (document.msExitFullscreen) {document.msExitFullscreen();
}
}
// 监听是否全屏
window.onload = function() {var elem = document.getElementById('state');
document.addEventListener('fullscreenchange',
function() {elem.innerText = document.fullscreen ? 'yes': 'no';},
false);
document.addEventListener('mozfullscreenchange',
function() {elem.innerText = document.mozFullScreen ? 'yes': 'no';},
false);
document.addEventListener('webkitfullscreenchange',
function() {elem.innerText = document.webkitIsFullScreen ? 'yes': 'no';},
false);
document.addEventListener('msfullscreenchange',
function() {elem.innerText = document.msFullscreenElement ? 'yes': 'no';},
false);
}