我正在开发一个应用程序,在这个应用程序中,当用户离开一个组件并进入另一个组件时,希望在其他组件的ngOnInit方法中实现这样一项功能:Chrome浏览器应该全屏显示,就像我们按下Fn + F11键一样。
非常感谢您提供任何帮助或参考资料。
我正在开发一个应用程序,在这个应用程序中,当用户离开一个组件并进入另一个组件时,希望在其他组件的ngOnInit方法中实现这样一项功能:Chrome浏览器应该全屏显示,就像我们按下Fn + F11键一样。
非常感谢您提供任何帮助或参考资料。
如何实现全屏 - https://www.w3schools.com/howto/howto_js_fullscreen.asp
以下是当前的 "Angular 方式"。
HTML
<h2 (click)="openFullscreen()">open</h2>
<h2 (click)="closeFullscreen()">close</h2>
组件
import { DOCUMENT } from '@angular/common';
import { Component, Inject, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(@Inject(DOCUMENT) private document: any) {}
elem;
ngOnInit() {
this.elem = document.documentElement;
}
openFullscreen() {
if (this.elem.requestFullscreen) {
this.elem.requestFullscreen();
} else if (this.elem.mozRequestFullScreen) {
/* Firefox */
this.elem.mozRequestFullScreen();
} else if (this.elem.webkitRequestFullscreen) {
/* Chrome, Safari and Opera */
this.elem.webkitRequestFullscreen();
} else if (this.elem.msRequestFullscreen) {
/* IE/Edge */
this.elem.msRequestFullscreen();
}
}
/* Close fullscreen */
closeFullscreen() {
if (this.document.exitFullscreen) {
this.document.exitFullscreen();
} else if (this.document.mozCancelFullScreen) {
/* Firefox */
this.document.mozCancelFullScreen();
} else if (this.document.webkitExitFullscreen) {
/* Chrome, Safari and Opera */
this.document.webkitExitFullscreen();
} else if (this.document.msExitFullscreen) {
/* IE/Edge */
this.document.msExitFullscreen();
}
}
}
requestFullscreen
。我已经在StackBlitz上创建了一个演示。fullScreen() {
let elem = document.documentElement;
let methodToBeInvoked = elem.requestFullscreen ||
elem.webkitRequestFullScreen || elem['mozRequestFullscreen']
||
elem['msRequestFullscreen'];
if (methodToBeInvoked) methodToBeInvoked.call(elem);
}
requestFullScreen
就足够了。
https://developer.mozilla.org/en-US/docs/Web/API/Element/requestFullScreen
这是一个示例,可切换全屏和普通屏幕:docElement: HTMLElement;
isFullScreen: boolean = false;
ngOnInit(): void {
this.docElement = document.documentElement;
}
toggleFullScreen() {
if (!this.isFullScreen) {
this.docElement.requestFullscreen();
}
else {
document.exitFullscreen();
}
this.isFullScreen = !this.isFullScreen;
}
<button (click)="toggleFullScreen()">SWITCH SCREEN MODE</button>
document
的 fullscreenElement
属性(https://developer.mozilla.org/en-US/docs/Web/API/Document/fullscreenElement)。 - Edu Müller将以下代码放在您想要触发的组件(@Component之前)的顶部:
interface FsDocument extends HTMLDocument {
mozFullScreenElement?: Element;
msFullscreenElement?: Element;
msExitFullscreen?: () => void;
mozCancelFullScreen?: () => void;
}
export function isFullScreen(): boolean {
const fsDoc = <FsDocument> document;
return !!(fsDoc.fullscreenElement || fsDoc.mozFullScreenElement || fsDoc.webkitFullscreenElement || fsDoc.msFullscreenElement);
}
interface FsDocumentElement extends HTMLElement {
msRequestFullscreen?: () => void;
mozRequestFullScreen?: () => void;
}
export function toggleFullScreen(): void {
const fsDoc = <FsDocument> document;
if (!isFullScreen()) {
const fsDocElem = <FsDocumentElement> document.documentElement;
if (fsDocElem.requestFullscreen)
fsDocElem.requestFullscreen();
else if (fsDocElem.msRequestFullscreen)
fsDocElem.msRequestFullscreen();
else if (fsDocElem.mozRequestFullScreen)
fsDocElem.mozRequestFullScreen();
else if (fsDocElem.webkitRequestFullscreen)
fsDocElem.webkitRequestFullscreen();
}
else if (fsDoc.exitFullscreen)
fsDoc.exitFullscreen();
else if (fsDoc.msExitFullscreen)
fsDoc.msExitFullscreen();
else if (fsDoc.mozCancelFullScreen)
fsDoc.mozCancelFullScreen();
else if (fsDoc.webkitExitFullscreen)
fsDoc.webkitExitFullscreen();
}
export function setFullScreen(full: boolean): void {
if (full !== isFullScreen())
toggleFullScreen();
}
在ngOnInit方法中调用setFullScreen(full: boolean)
函数:
ngOnInit(): void {
setFullScreen(true);
}
这是目前使用 Angular 的方法。
HTML: 在 HTML 中使用此代码:
(click)="openFullscreen()
NgOninit:
this.elem = document.documentElement;
TS:放入这个函数它就会工作…
openFullscreen() {
if (this.elem.requestFullscreen) {
this.elem.requestFullscreen();
} else if (this.elem.mozRequestFullScreen) {
/* Firefox */
this.elem.mozRequestFullScreen();
} else if (this.elem.webkitRequestFullscreen) {
/* Chrome, Safari and Opera */
this.elem.webkitRequestFullscreen();
} else if (this.elem.msRequestFullscreen) {
/* IE/Edge */
this.elem.msRequestFullscreen();
}`]`
}
您需要使用几乎所有浏览器都提供的全屏API
。您可以在这里阅读有关它的信息。
简而言之,要在全屏模式下显示一个元素
,您需要在其上调用requestFullscreen()
。通过exitFullscreen()
退出全屏模式。要检查浏览器是否处于全屏模式,可以访问fullscreenElement
文档属性。您还可以处理fullscreenchange
事件,以便在切换全屏模式时得到通知。
由于此API与基于Webkit的浏览器存在兼容性问题,因此建议您使用webkit
前缀。
如果您想以“Angular方式”实现全屏功能,建议创建一个指令。如果您愿意,可以使用第三方库,例如ngx-fullscreen-directive。