Cordova 螢幕方向外掛
Cordova 外掛,以通用的方式為 iOS、Android 和 windows-uwp 設定/鎖定螢幕方向。此外掛基於 螢幕方向 API,因此 API 與目前的規範相符。
此外掛會在 screen 物件 (window.screen
) 中新增以下項目
// lock the device orientation
.orientation.lock('portrait')
// unlock the orientation
.orientation.unlock()
// current orientation
.orientation
安裝
cordova plugin add cordova-plugin-screen-orientation
支援的方向
portrait-primary
方向為主要直向模式。
portrait-secondary
方向為次要直向模式。
landscape-primary
方向為主要橫向模式。
landscape-secondary
方向為次要橫向模式。
portrait
方向為 portrait-primary 或 portrait-secondary (感應器)。
landscape
方向為 landscape-primary 或 landscape-secondary (感應器)。
any
方向已解除鎖定 - 支援所有方向。
用法
// set to either landscape
screen.orientation.lock('landscape');
// allow user rotate
screen.orientation.unlock();
// access current orientation
console.log('Orientation is ' + screen.orientation.type);
事件
Android 和 iOS 都會在 window 物件上觸發 orientationchange 事件。如果需要通知,請針對此外掛版本使用 window 物件。
使用範例
window.addEventListener("orientationchange", function(){
console.log(screen.orientation.type); // e.g. portrait
});
「change」事件偵聽器也已新增至 screen.orientation 物件。
使用範例
screen.orientation.addEventListener('change', function(){
console.log(screen.orientation.type); // e.g. portrait
});
// OR
screen.orientation.onchange = function(){console.log(screen.orientation.type);
};
Android 說明
當手機 旋轉 180 度時,screen.orientation 屬性不會更新。
Windows UWP 說明
只有當裝置具有某種加速計時,Windows 商店應用程式 (windows-uwp) 才會顯示方向變更。「方向」的內部狀態仍會保留,但除非裝置支援,否則實際螢幕不會旋轉。