screenfull.js是什么
什么是screenfull.js,Simple wrapper for cross-browser usage of the JavaScript Fullscreen API
screenfull.js官网:官网
screenfull.js文档:文档
screenfull.js源码仓库:源码仓库
screenfull.js下载地址:点此下载 点此下载2
screenfull.js介绍、screenfull.js使用
Simple wrapper for cross-browser usage of the JavaScript Fullscreen API, which lets you bring the page or any element into fullscreen. Smoothens out the browser implementation differences, so you don't have to.
This package is feature complete. No new changes will be accepted.
Want to strengthen your core JavaScript skills and master ES6?
I would personally recommend this awesome ES6 course by Wes Bos.
Install
Only 0.7 kB gzipped.
$ npm install --save screenfull
Also available on cdnjs.
Why?
Screenfull
if (screenfull.enabled) {
screenfull.request();
}
Vanilla JavaScript
document.fullscreenEnabled = document.fullscreenEnabled || document.mozFullScreenEnabled || document.documentElement.webkitRequestFullScreen;
function requestFullscreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
}
}
if (document.fullscreenEnabled) {
requestFullscreen(document.documentElement);
}
// Actually it's more if you want it to work in Safari, but let's not go there...
Support
Documentation
Examples
Fullscreen the page
document.getElementById('button').addEventListener('click', () => {
if (screenfull.enabled) {
screenfull.request();
} else {
// Ignore or do something else
}
});
Fullscreen an element
const el = document.getElementById('target');
document.getElementById('button').addEventListener('click', () => {
if (screenfull.enabled) {
screenfull.request(el);
}
});
Fullscreen an element with jQuery
const target = $('#target')[0]; // Get DOM element from jQuery collection
$('#button').on('click', () => {
if (screenfull.enabled) {
screenfull.request(target);
}
});
Toggle fullscreen on a image with jQuery
$('img').on('click', event => {
if (screenfull.enabled) {
screenfull.toggle(event.target);
}
});
Detect fullscreen change
if (screenfull.enabled) {
screenfull.on('change', () => {
console.log('Am I fullscreen?', screenfull.isFullscreen ? 'Yes' : 'No');
});
}
Remove listeners with:
screenfull.off('change', callback);
Detect fullscreen error
if (screenfull.enabled) {
screenfull.on('error', event => {
console.error('Failed to enable fullscreen', event);
});
}
See the demo for more examples, and view the source.
Fullscreen an element with Angular.js
You can use the Angular.js binding to do something like:
This is a fullscreen element
Toggle fullscreen
Fullscreen the page with Angular 2
import {Directive, HostListener} from '@angular/core';
import * as screenfull from 'screenfull';
@Directive({
selector: '[toggleFullscreen]'
})
export class ToggleFullscreenDirective {
@HostListener('click') onClick() {
if (screenfull.enabled) {
screenfull.toggle();
}
}
}Toggle fullscreen
Methods
.request()
Make an element fullscreen.
Accepts a DOM element. Default is . If called with another element than the currently active, it will switch to that if it's a decendant.
If your page is inside an you will need to add a allowfullscreen attribute (+ webkitallowfullscreen and mozallowfullscreen).
Keep in mind that the browser will only enter fullscreen when initiated by user events like click, touch, key.
.exit()
Brings you out of fullscreen.
.toggle()
Requests fullscreen if not active, otherwise exits.
.on(event, function)
Events: change error
Add a listener for when the browser switches in and out of fullscreen or when there is an error.
.off(event, function)
Remove a previously registered event listener.
.onchange(function)
Alias for .on('change', function)
.onerror(function)
Alias for .on('error', function)
Properties
.isFullscreen
Returns a boolean whether fullscreen is active.
.element
Returns the element currently in fullscreen, otherwise null.
.enabled
Returns a boolean whether you are allowed to enter fullscreen. If your page is inside an `