SYNCERのロゴ
アイキャッチ画像

ScreenOrientation.lock() - ロックする

lock()は、ScreenOrientationのメソッドです。スクリーンの向きをある方向に固定します。

概要

名前
lock
所属
ScreenOrientation
IDL
Promise<void> lock(OrientationLockType orientation);

enum OrientationLockType {
    "any",
    "natural",
    "landscape",
    "portrait",
    "portrait-primary",
    "portrait-secondary",
    "landscape-primary",
    "landscape-secondary"
};
仕様書
https://www.w3.org/TR/screen-orientation/#dom-screenorientation-lock

説明

引数(orientation)には、許可する向きを、後述するリストの中から文字列で指定します。

返り値はPromiseです。向きの固定が完了したタイミングで関数が実行されます。渡される引数はありません。

フルスクリーン状態ではない場合や、向きの変更に対応していないデバイスではエラーが発生します。

"any"
"portrait-primary"、"portrait-secondary"、"landscape-primary"、"landscape-secondary"の状態を許可する。ロックしていないのと同じ。
"natural"
"portrait-primary"と"landscape-primary"の状態を許可する。そのデバイスのデフォルトの向きに固定される。
"landscape"
"landscape-primary"と"landscape-secondary"の状態を許可する。横向きに固定される。
"portrait"
"portrait-primary"と"portrait-secondary"の状態を許可する。縦向きに固定される。
"portrait-primary"
縦向きが標準のデバイスは0度、横向きが標準のデバイスは時計回りに90度、デフォルトから回転した向きに固定される。
"portrait-secondary"
縦向きが標準のデバイスは180度、横向きが標準のデバイスは反時計回りに90度、デフォルトから回転した向きに固定される。
"landscape-primary"
横向きが標準のデバイスは0度、縦向きが標準のデバイスは時計回りに90度、デフォルトから回転した向きに固定される。
"landscape-secondary"
横向きが標準のデバイスは180度、縦向きが標準のデバイスは反時計回りに90度、デフォルトから回転した向きに固定される。

デモ

ScreenOrientation.lock()のデモです。下部にある「OPEN」をクリックしてデモを新しいウィンドウで開いて下さい。そしてフルスクリーン状態にしてから、オリエンテーションの固定と解除を実行してみて下さい。

<!-- このコードは編集できます。 -->

<!DOCTYPE html>
<html>
<head>
<style>
body { word-break: break-all ; }
div#result { white-space: pre-wrap ; }
span#error { color: red ; }
</style>
</head>
<body>
<p><button id="requestFullscreen">フルスクリーンにする</button></p>
<p><button id="exitFullscreen">フルスクリーンを解除する</button></p>
<p><button id="lock">固定する</button></p>
<p><button id="unlock">解除する</button></p>
<hr>
<div id="result"></div>
<p>固定した回数→<span id="count">0</span></p>
<p>エラーの内容→<span id="error"></span></p>
<script>
var screenOrientation = screen.orientation ;

screenOrientation.onchange = getOrientationInfo ;

getOrientationInfo() ;

document.getElementById( "lock" ).onclick = function () {
	screenOrientation.lock( "landscape" ).then( resolveFn, rejectFn ) ;
}

document.getElementById( "unlock" ).onclick = function () {
	screenOrientation.unlock( "landscape" ) ;
}

function resolveFn () {
	document.getElementById( "count" ).textContent = ++number ;
}

function rejectFn ( reason ) {
	console.log( reason ) ;
	document.getElementById( "error" ).textContent = reason ;
}

function getOrientationInfo() {
	document.getElementById( "result" ).textContent = document.getElementById( "error" ).textContent = "" ;
	console.log( screenOrientation ) ;
	appendText( screenOrientation + "\n" ) ;
	appendText( "type: " + screenOrientation.type + "\n" ) ;
	appendText( "angle: " + screenOrientation.angle + "\n" ) ;
}

function appendText ( text ) {
	document.getElementById( "result" ).appendChild( document.createTextNode( text ) ) ;
}

var number = 0 ;

document.getElementById( "requestFullscreen" ).onclick = function () {
	var element = document.documentElement ;
	element.requestFullscreen = element.requestFullscreen || element.mozRequestFullScreen || element.webkitRequestFullscreen || element.msRequestFullscreen ;
	element.requestFullscreen() ;
}

document.getElementById( "exitFullscreen" ).onclick = function () {
	document.exitFullscreen = document.exitFullscreen || document.cancelFullScreen || document.mozCancelFullScreen || document.webkitCancelFullScreen || document.msExitFullscreen ;
	document.exitFullscreen() ;
}
</script>
</body>
</html>

サポート状況

ChromeFirefoxSafariEdgeIEOperaiOS SafariAndroid
38+ 43+××× 25+××
  • Twitterでシェア
  • Facebookでシェア
  • Google+でシェア
  • はてなブックマークでシェア
  • pocketに保存
  • LINEでシェア
更新履歴
2017年10月17日 (火)
コンテンツを公開しました。