デバイス
Framework7 には、デバイスとプラットフォームに関する有用な情報を含むデバイス検出ライブラリが付属しています。
Framework7 クラスの `device` プロパティ (`Framework7.device`) と、初期化されたアプリインスタンスの同じプロパティ (`app.device`) として利用できます。
// If we need it in place where we don't have access to app instance or before we init the app
if (Framework7.device.ios) {
console.log('It is iOS device');
}
// After we init the app we can access it as app instance property
var app = new Framework7({ /*...*/ });
if (app.device.android) {
console.log('It is android device');
}
getDevice
サーバーサイド環境または ES モジュールで Framework7 を使用する場合、代わりに `getDevice` ユーティリティを使用する必要があります。
import { getDevice } from 'framework7';
const device = getDevice();
if (device.android) {
console.log('It is android device');
}
サーバーサイドで使用する場合は、リクエストに基づいてわかっている場合に備えて、`userAgent` を渡すこともできます。
import { getDevice } from 'framework7';
const device = getDevice({ userAgent: 'SOME_USER_AGENT' });
if (device.android) {
console.log('It is android device');
}
メソッドとプロパティ
プロパティ | |
---|---|
ios | iOS デバイスの場合は `true` です。 |
android | Android デバイスの場合は `true` です。 |
androidChrome | Android Chrome の場合は `true` です。 |
desktop | デスクトップブラウザの場合は `true` です。 |
iphone | iPhone の場合は `true` です。 |
ipod | iPod の場合は `true` です。 |
ipad | iPad の場合は `true` です。 |
cordova | アプリが Cordova 環境で実行されている場合は `true` です。 |
capacitor | アプリが Capacitor 環境で実行されている場合は `true` です。 |
windows | デスクトップ Windows の場合は `true` です。 |
macos | デスクトップ macOS の場合は `true` です。 |
ie | Internet Explorer ブラウザの場合は `true` です。 |
edge | MS Edge ブラウザの場合は `true` です。 |
firefox | FireFox ブラウザの場合は `true` です。 |
electron | アプリが Electron 環境で実行されている場合は `true` です。 |
nwjs | アプリが NW.js 環境で実行されている場合は `true` です。 |
webView | アプリが Web ビューで実行されている場合に `true` です。 - ホーム画面にインストールされた Web アプリ、デスクトップにインストールされたデスクトップ PWA に有効です。 |
standalone | `webView` と同じです。 |
os | OS を含みます。 `ios`、`android`、`macos`、`windows` のいずれかです。 |
osVersion | OS バージョンを含みます。例: `11.2.0` |
pixelRatio | デバイスピクセル比 |
メソッド | |
prefersColorScheme() | ユーザーシステムの優先カラースキームを返します。この機能がサポートされている場合は `light` または `dark` を返し、それ以外の場合は `undefined` を返します。この機能のサポートは、`(prefers-color-scheme)` メディアクエリのサポートに基づいています。 |
デバイス関連クラス
デバイス検出ライブラリは、`<html>` 要素に追加のクラスを追加します。これにより、異なる OS やプラットフォームに対して異なる CSS スタイルを使用できます。
つまり、クラスは次のルールで計算されます。
device-[os]
device-desktop
device-macos - if desktop macOS device
device-windows - if desktop windows device
device-pixel-ratio-[pixel ratio]
device-cordova - if app running under cordova