Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simple wrapper for cross-browser usage of the JavaScript Fullscreen API

License

NotificationsYou must be signed in to change notification settings

sindresorhus/screenfull

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.

Not supported on iPhone

This package is feature complete. No new features will be accepted.

Install

Only 0.7 kB gzipped.

npm install screenfull

Also available oncdnjs(older version).

Why?

Screenfull

importscreenfullfrom'screenfull';if(screenfull.isEnabled){screenfull.request();}

Vanilla JavaScript

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.

Support

Supported browsers

Note: Safari is supported on desktop and iPad, but not on iPhone. This is a limitation in the browser, not in Screenfull.

Documentation

Examples

Fullscreen the page

importscreenfullfrom'screenfull';document.getElementById('button').addEventListener('click',()=>{if(screenfull.isEnabled){screenfull.request();}else{// Ignore or do something else}});

Fullscreen an element

importscreenfullfrom'screenfull';constelement=document.getElementById('target');document.getElementById('button').addEventListener('click',()=>{if(screenfull.isEnabled){screenfull.request(element);}});

Hide navigation user-interface on mobile devices

importscreenfullfrom'screenfull';constelement=document.getElementById('target');document.getElementById('button').addEventListener('click',()=>{if(screenfull.isEnabled){screenfull.request(element,{navigationUI:'hide'});}});

Fullscreen an element with jQuery

importscreenfullfrom'screenfull';constelement=$('#target')[0];// Get DOM element from jQuery collection$('#button').on('click',()=>{if(screenfull.isEnabled){screenfull.request(element);}});

Toggle fullscreen on a image with jQuery

importscreenfullfrom'screenfull';$('img').on('click',event=>{if(screenfull.isEnabled){screenfull.toggle(event.target);}});

Detect fullscreen change

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);

Detect fullscreen error

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.

Fullscreen an element with Angular.js

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>

Fullscreen the page with Angular 2

import{Directive,HostListener}from'@angular/core';importscreenfullfrom'screenfull';@Directive({selector:'[toggleFullscreen]'})exportclassToggleFullscreenDirective{@HostListener('click')onClick(){if(screenfull.isEnabled){screenfull.toggle();}}}
<buttontoggleFullscreen>Toggle fullscreen<button>

API

.request(element, options?)

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.

.exit()

Brings you out of fullscreen.

Returns a promise that resolves after the element exits fullscreen.

.toggle(element, options?)

Requests fullscreen if not active, otherwise exits.

Accepts a DOM element andFullscreenOptions.

Returns a promise that resolves after the element enters/exits fullscreen.

.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)

.isFullscreen

Returns a boolean whether fullscreen is active.

.element

Returns the element currently in fullscreen, otherwiseundefined.

.isEnabled

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).

.raw

Exposes the raw properties (prefixed if needed) used internally:requestFullscreen,exitFullscreen,fullscreenElement,fullscreenEnabled,fullscreenchange,fullscreenerror

FAQ

How can I navigate to a new page when fullscreen?

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';});

Resources

About

Simple wrapper for cross-browser usage of the JavaScript Fullscreen API

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors25


[8]ページ先頭

©2009-2025 Movatter.jp