javascript - 打开全屏模式后查找元素大小
问题描述
我通过以下方式切换全屏模式
$("#myCanvas").webkitRequestFullscreen()
开启全屏模式后如何找到画布的大小?
我试图听“resize”事件以及“webkitfullscreenchange”,但是当他们调度时,画布大小还不是全屏的。
解决方案
我会把它放在评论中......但现在看起来有点乱...... xD。
如果您要全屏显示,那么您想要的不仅仅是屏幕的实际尺寸吗?这个window.screen
对象对你有用吗?EGwindow.screen.width
或window.screen.availWidth
?
https://developer.mozilla.org/en-US/docs/Web/API/Window/screen
在我的控制台中使用它一段时间,我的假设是 fullscreenchange 事件在样式重新计算和更改的绘制事件之前被触发。在 chrome 中,我能够通过将我的代码放在双 requestAnimationFrame 调用中来报告正确的大小(从而将代码推迟到下一帧)。
所以你的代码可能看起来像......
document.onwebkitfullscreenchange = ((evt) => {
requestAnimationFrame(() => {
requestAnimationFrame(() => {
//code you need to calculate the size of the element here
console.log(document.querySelector('.container').getBoundingClientRect().width);
});
});
});
至少桌面上的 chrome 68 也是如此。YMMV。
推荐阅读
- angular - 无法将 Angular cli 降级到 1.0.0-Beta.24
- c# - 动态分配字段
- wpf - 如何在 MVVM Light 中使用 SimpleIoc 将数组添加到参数中?
- javascript - 如何在不禁用 Chrome 中的站点隔离的情况下让 Apps Script Web App 工作?
- javascript - 使用 google sheet 脚本编辑器将数据提交到不同的选项卡
- java - 使用 Akka Persistence,如何将数据持久保存到 Oracle 表中
- angular - Angular Memory Leak via DebugElement - 如何在生产模式下运行而不混淆?
- sqlite - 在子查询上加入 Sqlite 表
- c - 读取函数可以读取多少字节?
- javascript - Facebook像素购买体事件