设备
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 模块时,我们需要使用getDevice
util 而不是:
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 | true 对于 iOS 设备 |
android | true 对于 Android 设备 |
androidChrome | true 对于 Android Chrome |
desktop | true 对于桌面浏览器 |
iphone | true 对于 iPhone |
ipod | true 对于 iPod |
ipad | true 对于 iPad |
cordova | true 当应用在 cordova 环境中运行时 |
capacitor | true 当应用在 capacitor 环境中运行时 |
windows | true 对于桌面 Windows |
macos | true 对于桌面 macOS |
ie | true 对于 Internet Explorer 浏览器 |
edge | true 对于 MS Edge 浏览器 |
firefox | true 对于 FireFox 浏览器 |
electron | true 当应用在 Electron 环境下运行时 |
nwjs | true 当应用在 NW.js 环境下运行时 |
webView | true 如果应用在 webView 中运行 - webapp 安装到主屏幕,适用于桌面 PWAs 安装到桌面 |
standalone | 与webView |
os | 包含可ios , android , macos , windows |
osVersion | 包含 OS 版本,例如11.2.0 |
pixelRatio | 设备像素比 |
方法 | |
prefersColorScheme() | 返回首选用户系统配色方案。返回"light" 或"dark" 如果此功能受支持或undefined 否则。此功能支持基于(prefers-color-scheme) media query 支持. |
设备相关类
设备检测库在元素上添加了额外的类,这些类可以帮助您为不同的 OS 和平台应用不同的 CSS 样式。<html>
element which can help you with different CSS styles for different OS and platforms.
换句话说,类按以下规则计算:
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