通过 javascript 将窗口设置为全屏(真正的全屏;F11 功能)
关于这一点有几个问题,有些人说这是不可能的,有些人说它在 IE 中是可能的,例如 Internet Explorer 全屏模式? 我想知道一个通用的解决方案和答案。
我正在构建一个照片库网页,当全屏查看时,该库确实会产生不同的效果(正如标题所说,我说的是真正的全屏,而不是带有栏和窗口镶边等),并且我想放置一个全屏按钮。 (不,我不会在没有用户意图的情况下强行进行 FS,我也讨厌这种“功能”)在 Flash 中,当通过用户启动的操作(例如按钮单击)启动时,这是可能的,我想知道这样的操作是否可行Javascript 也可用。从逻辑上讲,它应该具有类似于 Flash/SL 用户启动的全屏模式的机制。如果没有适用于所有人的“通用”功能,我可以(总比没有好)部分功能(我的意思是支持某些浏览器,而不是设置窗口宽度/height 等没有给出设置窗口宽度/高度的答案,我知道如何做到这一点,我也不是在寻找它)。
There are several questions about this, some say it's not possible, some say it IS possible in IE such as Internet Explorer full screen mode? and I'm wondering a universal solution and an answer for this.
I'm building a photo gallery webpage, and the gallery really makes out a difference when viewed fullscreen (as the title says, I am talking about true fullscreen, not with bars and window chrome etc), and I would like to place a button for fullscreen. (no, I won't be going forcefully FS without user's intention, I hate that kind of "functionality" too) It IS possible in Flash when initiated through a user-initiated action such as button click, and I was wondering if such a thing is available for Javascript too. Logically, it should have a mechanism similar to Flash/SL user initiated fullscreen mode. If there's no "universal" functionality that will work for all, I'm ok (better than nothing) for partial functionality (I mean supporting SOME of the browsers by that, NOT setting window width/height etc. don't come with answer telling to set window width/height, I know how to do it, I'm NOT looking for it) too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
现在,最新版本的 Chrome、Firefox 和 IE(11) 都可以实现这一点。
按照 Zuul 在此线程上的指示,我编辑了他的代码以包含 IE11 和选项全屏显示页面上选择的任何元素。
JS:
HTML:
其中“document.body”是您希望的任何元素。
另请注意,尝试从控制台运行这些全屏命令似乎不适用于 Chrome 或 IE。不过我在 Firefox 中使用 Firebug 确实取得了成功。
另一件需要注意的事情是,这些“全屏”命令没有垂直滚动条,您需要在 CSS 中指定这一点:
“!important”似乎是 IE 渲染它所必需的
这是一个工作示例。
对于任何想要编辑此内容并将其转换为代码片段的人来说,请注意,不要打扰。该代码无法在 SO 代码片段中工作,因为它将其放在
iframe
中。This is now possible in the latest versions of Chrome, Firefox and IE(11).
Following the pointers by Zuul on this thread, I edited his code to include IE11 and the option to full screen any element of choice on your page.
JS:
HTML:
Where "document.body" is any element you so wish.
Also note that trying to run these full screen commands from the console do not appear to work on Chrome or IE. I did have success with Firebug in Firefox though.
One other thing to note is that these "full screen" commands don't have a vertical scrollbar, you need to specify this within the CSS:
The "!important" seems to be necessary for IE to render it
Here's an example of it working.
A quick note for anyone wanting to edit this and turn it into a code snippet, don't bother. The code doesn't work from within SO code snippets because it puts it within an
iframe
.不可以。旧版 IE (≤6) 允许,但此功能被视为安全问题,因此现代浏览器都不允许使用它。您仍然可以调用
window.open(url,'','fullscreen=yes')
,它可以完成 90% 的任务,但结果略有不同:这与您使用 JavaScript 所能得到的最接近。您的另一个选择是在 Flash 中构建一些东西(呃!),或者只是弹出“全屏”按钮打开一个显示“按 F11 进入全屏”的灯箱,然后通过window.resize
或单击灯箱中的取消按钮隐藏该灯箱。编辑:正确的全屏API(第一个由 Mozilla 提出,后来作为 W3C 提案)已由 Webkit (Safari 5.1+/Chrome 15+) 实施,火狐浏览器(10+)。 这里有一个简短的历史和使用示例。请注意,IE10 据称不会 支持API。
No. Older versions of IE (≤6) allowed it, but this functionality is seen as a security problem, so no modern browser allows it.You can still call
window.open(url,'','fullscreen=yes')
, which gets you 90% of the way there, but has slightly different results:This is as close as you'll get with JavaScript.Your other option would be to build something in Flash (ugh!), or just have your "fullscreen" button pop up a lightbox that says "Press F11 to go fullscreen", and hide the lightbox onwindow.resize
or clicking a cancel button in the lightbox.Edit: A proper fullscreen API (first proposed by Mozilla and later released as a W3C proposal) has been implemented by Webkit (Safari 5.1+/Chrome 15+) and Firefox (10+). A brief history and usage examples here. Note that IE10 will allegedly not support the API.
我想知道为什么没有人注意到所有答案都是错误的。
将 body 元素设置为全屏不具有与按 F11 相同的行为。
F11 的相同行为可以通过以下方式获得:
并且
为了检查我们是否处于全屏模式,我使用此行:
这还检测 F11 是否调用了全屏:
注意:
这必须从用户交互事件(onclick、onkeydown 等)中调用。
注意 2:当用户按 F11 时,没有“元素”真正设置为全屏,因此检测该元素的唯一方法是用事件侦听器拦截键盘或检查客户端尺寸是否与屏幕尺寸相同**
另外:
I wonder why nobody noticed that all answers are wrong.
Setting the body element to full screen does not have the same behaviour of pressing F11.
The same behaviour of F11 can be obtained by:
and
also to check if we are in full screen mode I use this line:
this also detects if fullScreen was invoked by F11:
Note:
this must be called from within a user interaction event (onclick, onkeydown, etc).
Note 2: when the user presses F11 no "element" is really set in full screen so the only way to detect that is to intercept the keyboard with an eventlistener or to check if the client dimensions are the same of the screen dimensions**
Also:
您可以使用已签名的 Java 小程序来执行此操作,该小程序有权运行自动化脚本以发出击键以进入全屏模式。但是,这完全是一种黑客行为,除非您的用户不介意您的网站操纵他们的机器,否则不太实用。
You can do this with a signed java applet that has permission to run an automation script to issue the keystroke to go into fullscreen mode. But, this is a total hack that wouldn't be very practical unless your users don't mind your site manipulating their machines.
有一个未知的库可以为您完成所有工作:
https://github.com/rafrex/fscreen
我也遇到了同样的问题(例如在 React 组件中):
适用于 Chrome、Firefox、Safari、IE11、iOS、Android
There's an unknown library that makes all the work for you:
https://github.com/rafrex/fscreen
I had the same issue and I did (for instance in a react component):
Works in Chrome, Firefox, Safari, IE11, iOS, Android
Chrome 或 Firefox 未实现通过 java 脚本实现全屏支持。不过,您可以设法将其用于 MSIE。但这也不是 F11 的功能。
即使 chrome.exe -kiosk 也不会以全屏模式打开页面。
原因是不建议强制用户以全屏模式打开应用程序。如果不是这样,来自不同网站的所有弹出窗口都会以全屏模式打开,您最终将关闭所有这些弹出窗口。
Full screen support through java script is not implemented for Chrome or Firefox. However you can manage to have it for MSIE. But that is also not F11 kind of functionality.
Even
chrome.exe -kiosk
does not open page in fullscreen mode.Reason is that it is not recommended to force user and open your application in fullscreen mode. If this is not all the popups from different websites will open in fullscreen mode and you will endup closing all those.