Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork691
Simple wrapper for cross-browser usage of the JavaScript Fullscreen API
License
sindresorhus/screenfull
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Simple wrapper for cross-browser usage of the JavaScriptFullscreen 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 ESM. Pleasefamiliarize yourself with what that implies.
If you cannot use ESM or need to support older browsers without using transpilation, use version 5.2.0.
This package is feature complete. No new features will be accepted.
Only 0.7 kB gzipped.
npm install screenfull
Also available oncdnjs(older version).
importscreenfullfrom'screenfull';if(screenfull.isEnabled){screenfull.request();}
document.fullscreenEnabled=document.fullscreenEnabled||document.mozFullScreenEnabled||document.documentElement.webkitRequestFullScreen;functionrequestFullscreen(element){if(element.requestFullscreen){element.requestFullscreen();}elseif(element.mozRequestFullScreen){element.mozRequestFullScreen();}elseif(element.webkitRequestFullScreen){element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);}}if(document.fullscreenEnabled){requestFullscreen(document.documentElement);}// This is not even entirely comprehensive. There's more.
Note: Safari is supported on desktop and iPad, but not on iPhone. This is a limitation in the browser, not in Screenfull.
importscreenfullfrom'screenfull';document.getElementById('button').addEventListener('click',()=>{if(screenfull.isEnabled){screenfull.request();}else{// Ignore or do something else}});
importscreenfullfrom'screenfull';constelement=document.getElementById('target');document.getElementById('button').addEventListener('click',()=>{if(screenfull.isEnabled){screenfull.request(element);}});
importscreenfullfrom'screenfull';constelement=document.getElementById('target');document.getElementById('button').addEventListener('click',()=>{if(screenfull.isEnabled){screenfull.request(element,{navigationUI:'hide'});}});
importscreenfullfrom'screenfull';constelement=$('#target')[0];// Get DOM element from jQuery collection$('#button').on('click',()=>{if(screenfull.isEnabled){screenfull.request(element);}});
importscreenfullfrom'screenfull';$('img').on('click',event=>{if(screenfull.isEnabled){screenfull.toggle(event.target);}});
importscreenfullfrom'screenfull';if(screenfull.isEnabled){screenfull.on('change',()=>{console.log('Am I fullscreen?',screenfull.isFullscreen ?'Yes' :'No');});}
Remove listeners with:
importscreenfullfrom'screenfull';screenfull.off('change',callback);
importscreenfullfrom'screenfull';if(screenfull.isEnabled){screenfull.on('error',event=>{console.error('Failed to enable fullscreen',event);});}
See thedemo for more examples, and view the source.
You can use theAngular.js binding to do something like:
<divngsf-fullscreen><p>This is a fullscreen element</p><buttonngsf-toggle-fullscreen>Toggle fullscreen</button></div>
import{Directive,HostListener}from'@angular/core';importscreenfullfrom'screenfull';@Directive({selector:'[toggleFullscreen]'})exportclassToggleFullscreenDirective{@HostListener('click')onClick(){if(screenfull.isEnabled){screenfull.toggle();}}}
<buttontoggleFullscreen>Toggle fullscreen<button>
Make an element fullscreen.
Accepts a DOM element andFullscreenOptions.
The default element is<html>. If called with another element than the currently active, it will switch to that if it's a descendant.
If your page is inside an<iframe> you will need to add aallowfullscreen attribute (+webkitallowfullscreen andmozallowfullscreen).
Keep in mind that the browser will only enter fullscreen when initiated by user events like click, touch, key.
Returns a promise that resolves after the element enters fullscreen.
Brings you out of fullscreen.
Returns a promise that resolves after the element exits fullscreen.
Requests fullscreen if not active, otherwise exits.
Accepts a DOM element andFullscreenOptions.
Returns a promise that resolves after the element enters/exits fullscreen.
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, otherwiseundefined.
Returns a boolean whether you are allowed to enter fullscreen. If your page is inside an<iframe> you will need to add aallowfullscreen attribute (+webkitallowfullscreen andmozallowfullscreen).
Exposes the raw properties (prefixed if needed) used internally:requestFullscreen,exitFullscreen,fullscreenElement,fullscreenEnabled,fullscreenchange,fullscreenerror
That's not supported by browsers for security reasons. There is, however, a dirty workaround. Create a seamless iframe that fills the screen and navigate to the page in that instead.
importscreenfullfrom'screenfull';document.querySelector('#new-page-button').addEventListener(()=>{constiframe=document.createElement('iframe')iframe.setAttribute('id','external-iframe');iframe.setAttribute('src','https://new-page-website.com');iframe.setAttribute('frameborder','no');iframe.style.position='absolute';iframe.style.top='0';iframe.style.right='0';iframe.style.bottom='0';iframe.style.left='0';iframe.style.width='100%';iframe.style.height='100%';document.body.prepend(iframe);document.body.style.overflow='hidden';});
About
Simple wrapper for cross-browser usage of the JavaScript Fullscreen API
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.