ScreenFull-ES6 based on 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.
$ npm install --save screenfull-es6
Also available on unpkg.com or jsdelivr.
document.getElementById('button').addEventListener('click', () => {
if (ScreenFull.enabled) ScreenFull.request();
});
const el = document.getElementById('target');
document.getElementById('button').addEventListener('click', () => {
if (ScreenFull.enabled) ScreenFull.request(el);
});
const target = $('#target')[0]; // Get DOM element from jQuery collection
$('#button').on('click', () => {
if (ScreenFull.enabled) ScreenFull.request(target);
});
if (ScreenFull.enabled) {
ScreenFull.on('change', () => {
console.log('Am I fullscreen?', ScreenFull.isFullscreen ? 'Yes' : 'No');
});
}
Remove listeners with:
ScreenFull.off('change', callback);
if (ScreenFull.enabled) {
ScreenFull.on('error', event => {
console.error('Failed to enable fullscreen', event);
});
}
See the demo for more examples, or view the source.
import {Directive, HostListener} from '@angular/core';
import ScreenFull from 'screenfull-es6';
@Directive({
selector: '[toggleFullscreen]'
})
export class ToggleFullscreenDirective {
@HostListener('click') onClick() {
if (ScreenFull.enabled) {
ScreenFull.toggle();
}
}
}
<button toggleFullscreen>Toggle fullscreen<button>
<template>
<button v-on:click="toggleFullscreen">Toggle fullscreen</button>
</template>
<script>
import ScreenFull from 'screenfull-es6';
export default {
methods: {
toggleFullscreen(){
if (ScreenFull.enabled) {
ScreenFull.toggle();
}
}
}
}
</script>
Make an element fullscreen.
Accepts a DOM element. Default is <html>
. 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 <iframe>
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.
Brings you out of fullscreen.
Requests fullscreen if not active, otherwise exits.
Events: change
error
Add a listener for when the browser switches in and out of fullscreen or when there is an error.
Remove a previously registered event listener.
Alias for .on('change', function)
Alias for .on('error', function)
Returns a boolean whether fullscreen is active.
Returns the element currently in fullscreen, otherwise null
.
Returns a boolean whether you are allowed to enter fullscreen. If your page is inside an <iframe>
you will need to add a allowfullscreen
attribute (+ webkitallowfullscreen
and mozallowfullscreen
).
MIT © Sindre Sorhus, modified by Aleksandr Tar