LoginSignup
7
9

More than 5 years have passed since last update.

Fullscreen API をさわったのでメモ

Posted at

document.querySelector('img').requestFullScreen()とかすると画像が全画面表示されるやつ。画面ロード直後にいきなり全画面表示とかはできないようになってるのでclickイベント内とかで行う。

詳しくは以下

画像ギャラリー的なUIでの利用を想定してみる。画像をクリックしたら最大化表示されるやつ。

<style>
img{
  width: 300px;
  height: 233px;
  border: solid 4px #aaa;
  border-radius: 16px;
}
img:hover{
  border-color: #00aaff;
  cursor: pointer;
}
</style>
</head>
<body>
<h1>FullScreen Demo for Chrome/Safari</h1>
<img src="https://lh5.googleusercontent.com/-7cggVHG-i74/U5M05ee0DAI/AAAAAAAAHWA/iM3hb0gONVc/s800/IMG_1727.png"/>
<img src="https://lh4.googleusercontent.com/-E-GSa8wymBs/U5MzX0_5G7I/AAAAAAAAHVM/S2igEXSg8nU/s800/IMG_1778.png"/>
<script>
var images = document.querySelectorAll('img');
Array.prototype.forEach.call(images, function (image, index, nodeList) {
  image.addEventListener('click', function(){
    if(image.webkitRequestFullScreen) {
      image.webkitRequestFullScreen();
    }
  });
});

gallery.png

最大化されない

Firefoxの場合、styleのサイズ指定が無視されでっかく表示されるけど、Chrome/Safariの場合は小さいままになってしまう。

Firefox

firefox_full.png

Chrome/Safari

chrome_full.png

MDNによると以下でFirefoxと同じ挙動になるとのこと。

img:-webkit-full-screen {
  width: 100%;
  height: 100%;
}

なんか長細くて変w

chrome_full2.png

FullScreen化された時だけ、サムネイル時のstyleを解除するといい感じに動くみたい。

<style>
.noFullScreen img{
  width: 300px;
  height: 233px;
  border: solid 4px #aaa;
  border-radius: 16px;
  display: inline-block;
}
.noFullScreen img:hover{
  border-color: #00aaff;
  cursor: pointer;
}
</style>
<script>
document.addEventListener('webkitfullscreenchange', function(){
  var image = document.webkitFullscreenElement;
  if(image){
    document.body.classList.remove('noFullScreen');
  }
  else{
    document.body.classList.add('noFullScreen');
  }
}, false);
</script>

chrome_full3.png

Chromeの場合のみ最大化解除後、段積みになっちゃう

Chromeの場合のみ以下みたいになってしまう。バグかな。

gallery_chrome.png

これについてはinline-blockを指定すれば解決するっぽい。

.noFullScreen img{
  width: 300px;
  height: 233px;
  border: solid 4px #aaa;
  border-radius: 16px;
  display: inline-block; /* 追加 */
}
7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9