Using the Fullscreen API

栏目: IT技术 · 发布时间: 5年前

内容简介:In this article, we’ll cover theTheIt’s really easy to activate fullscreen mode on the web! Currently, some browsers still require prefixing the

In this article, we’ll cover the Fullscreen API with ample amounts of code snippets, and interactive demos. It’s a suprisingly easy API to work with!

The Fullscreen API was introduced in 2013, and provides a native way for browsers to enter/exit fullscreen mode. This specification brought a slew of JavaScript APIs, and CSS selectors that we can use to refine this immersive user experience.

Basics of the Fullscreen API

It’s really easy to activate fullscreen mode on the web! Currently, some browsers still require prefixing the requestFullscreen method.

To check which Fullscreen API method is available, you can create a helper function similar to this:

function activateFullscreen(element) {
  if(element.requestFullscreen) {
    element.requestFullscreen();        // W3C spec
  }
  else if (element.mozRequestFullScreen) {
    element.mozRequestFullScreen();     // Firefox
  }
  else if (element.webkitRequestFullscreen) {
    element.webkitRequestFullscreen();  // Safari
  }
  else if(element.msRequestFullscreen) {
    element.msRequestFullscreen();      // IE/Edge
  }
};

The word "request" in requestFullscreen is due to the fact that browsers don't have permissions (by default) to activate fullscreen mode.

— Sorry to interrupt this program!

If you're interested in learning JavaScript in a comprehensive and structured way, I highly recommend you try Wes Bos' Beginner JavaScript or ES6+ for Everyone course. Learning from a premium course like that is a serious investment in yourself.

Plus, these areaffiliate links, so if you purchase a course you help Alligator.io continue to exist at the same time! :pray:

- Seb, :v:+:heart:

Exiting fullscreen is pretty easy, but it also requires a bit of browser detection:

function deactivateFullscreen() {
  if(document.exitFullscreen) {
    document.exitFullscreen();
  } else if (document.mozCancelFullScreen) {
    document.mozCancelFullScreen();
  } else if (document.webkitExitFullscreen) {
    document.webkitExitFullscreen();
  }
};

Using the above function, to activate fullscreen, simply pass the document HTMLElement!

<button
  onclick="activateFullscreen(document.documentElement);"
>
  Go fullscreen!
</button>

<button
  onclick="deactivateFullscreen();"
>
  Leave fullscreen
</button>

See the Pen alligatorio-fullscreen-api-1 by wle8300 ( @wle8300 ) on CodePen .

Using Fullscreen API on unconventional HTMLElements

As you might have guessed, other HTML hlements can go into fullscreen mode too, not just document !

In the demo below, try clicking the buttons to make <h1> , <img> , and <video> go into fullscreen mode:

<button
  onclick="activateFullscreen(document.getElementById('my-image'));"
>
  Fullscreen #my-text!
</button>
<button
  onclick="activateFullscreen(document.getElementById('my-image'))"
>
  Fullscreen #my-image
</button>
<button
  onclick="activateFullscreen(document.getElementById('my-video'))"
>
  Fullscreen #my-video
</button>

<h1 id="my-text">Hello world</h1>
<img id="my-image" src="alligatorio-logo.svg" width="200"/>
<video id="my-video" controls src="big-buck-bunny.mp4" />

See the Pen alligatorio-fullscreen-api-2 by wle8300 ( @wle8300 ) on CodePen .

Properties & Events

There are additional properties that Fullscreen API brings:

  • document.fullScreenEnabled : returns a boolean denoting whether the webpage has permissions to enter fullscreen mode
  • document.fullScreenElement : returns a HTMLElement Node (only when fullscreen is activated)

You’ll also need to detect the browser:

const fullscreenEnabled = document.fullscreenEnabled
  || document.mozFullScreenEnable
  || document.webkitFullscreenEnabled;
const fullscreenElement = document.fullscreenElement
  || document.mozFullScreenElemen
  || document.webkitFullscreenElement;

There’s also an event called fullscreenchange when the user enters/exits fullscreen mode that you can listen to:

const fullscreenElement = document.fullscreenElement
  || document.mozFullScreenElement
  || document.webkitFullscreenElement;

document.addEventListener('fullscreenchange', (event) => {
  if (fullscreenElement) {
    console.log('Entered fullscreen:', document.fullscreenElement);
  } else {
    console.log('Exited fullscreen.');
  }
});

Styling Fullscreen Elements

On top of the JavaScript API that’s available, there’s also a few CSS pseudo-classes you can use:

/* Targets the
  HTML element that's
  in fullscreen mode */
:fullscreen,
:-webkit-full-screen,
:-moz-full-screen,
:-ms-fullscreen {
  /* ... */
}


/* Styling the
  backdrop */
::backdrop {
  /* ... */
}

Here’s an example where we add a groovy background-color , and opacity rules to the backdrop:

::backdrop {
  opacity: 0.8;
  background: #DFA612;
}

See the Pen alligatorio-fullscreen-api-3 by wle8300 ( @wle8300 ) on CodePen .

Try clicking the button! You can read more about :fullscreen and :backdrop on the Mozilla Developer Network.

The W3C specification alternated between "fullscreen" and "full-screen" so you'll see a discrepancy in older specs, browser prefixes, etc. Going forward browsers will stick with "fullscreen"

Can I Use fullscreen? Data on support for the fullscreen feature across the major browsers from caniuse.com.


以上所述就是小编给大家介绍的《Using the Fullscreen API》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

机器学习

机器学习

(美)Tom Mitchell / 曾华军、张银奎、等 / 机械工业出版社 / 2008-3 / 35.00元

《机器学习》展示了机器学习中核心的算法和理论,并阐明了算法的运行过程。《机器学习》综合了许多的研究成果,例如统计学、人工智能、哲学、信息论、生物学、认知科学、计算复杂性和控制论等,并以此来理解问题的背景、算法和其中的隐含假定。《机器学习》可作为计算机专业 本科生、研究生教材,也可作为相关领域研究人员、教师的参考书。一起来看看 《机器学习》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具