https://github.com/matthewhudson/current-device

CURRENT-DEVICE

      

This module makes it easy to write conditional CSS and/or JavaScript based on device operating system (iOS, Android, Blackberry, Windows, MacOs, Firefox OS, MeeGo, AppleTV, etc), orientation (Portrait vs. Landscape), and type (Tablet vs. Mobile).

View the Demo →

EXAMPLES

This module inserts CSS classes into the <html> element.

iPhone

Android Tablet

Blackberry Tablet

DEVICE SUPPORT

  • iOS: iPhone, iPod, iPad
  • Android: Phones & Tablets
  • Blackberry: Phones & Tablets
  • Windows: Phones & Tablets
  • Firefox OS: Phones & Tablets

USAGE

Just include the script. The script then updates the <html> section with the appropriate classes based on the device's characteristics.

Installation

npm install current-device

And then import it:

// using es modules
import device from "current-device"; // common.js
const device = require("current-device").default;

Or use script tags and globals.

<script src="https://unpkg.com/current-device/umd/current-device.min.js"></script>

And then access it off the global like so:

console.log("device.mobile() === %s", device.mobile());

CONDITIONAL CSS

The following tables map which CSS classes are added based on device and orientation.

Device CSS Class Names

Device CSS Classes
iPad ios ipad tablet
iPhone ios iphone mobile
iPod ios ipod mobile
Android Phone android mobile
Android Tablet android tablet
BlackBerry Phone blackberry mobile
BlackBerry Tablet blackberry tablet
Windows Phone windows mobile
Windows Tablet windows tablet
Firefox OS Phone fxos mobile
Firefox OS Tablet fxos tablet
MeeGo meego
Desktop desktop
Television television

Orientation CSS Class Names

Orientation CSS Classes
Landscape landscape
Portrait portrait

CONDITIONAL JAVASCRIPT

This module also includes support for conditional JavaScript, allowing you to write checks on the following device characteristics:

Device JavaScript Methods

Device JavaScript Method
Mobile device.mobile()
Tablet device.tablet()
Desktop device.desktop()
iOS device.ios()
iPad device.ipad()
iPhone device.iphone()
iPod device.ipod()
Android device.android()
Android Phone device.androidPhone()
Android Tablet device.androidTablet()
BlackBerry device.blackberry()
BlackBerry Phone device.blackberryPhone()
BlackBerry Tablet device.blackberryTablet()
Windows device.windows()
Windows Phone device.windowsPhone()
Windows Tablet device.windowsTablet()
Firefox OS device.fxos()
Firefox OS Phone device.fxosPhone()
Firefox OS Tablet device.fxosTablet()
MeeGo device.meego()
Television device.television()

Orientation JavaScript Methods

Orientation JavaScript Method
Landscape device.landscape()
Portrait device.portrait()

Orientation JavaScript Callback

device.onChangeOrientation(newOrientation => {
console.log(`New orientation is ${newOrientation}`);
});

Utility Methods

device.noConflict()

Run current-device in noConflict mode, returning the device variable to its previous owner. Returns a reference to the device object.

const currentDevice = device.noConflict();

Useful Properties

Access these properties on the device object to get the first match on that attribute without looping through all of its getter methods.

JS Property Returns
device.type 'mobile', 'tablet', 'desktop', or 'unknown'
device.orientation 'landscape', 'portrait', or 'unknown'
device.os 'ios', 'iphone', 'ipad', 'ipod', 'android', 'blackberry', 'windows', 'macos', 'fxos', 'meego', 'television', or 'unknown'

BEST PRACTICES

Environment detection has a high rate of misuse. Often times, folks will attempt to work around browser feature support problems by checking for the affected browser and doing something different in response. The preferred solution for those kinds of problems, of course, is to check for the feature, not the browser (ala Modernizr).

However, that common misuse of device detection doesn't mean it should never be done. For example, current-device could be employed to change the interface of your web app such that it uses interaction patterns and UI elements common to the device it's being presented on. Android devices might get a slightly different treatment than Windows or iOS, for instance. Another valid use-case is guiding users to different app stores depending on the device they're using.

In short, check for features when you need features, and check for the browser when you need the browser.

Contributors

Thanks goes to these wonderful people (emoji key):


Matthew Hudson

 

Rafael Terán


Allan


martinwepner

This project follows the all-contributors specification. Contributions of any kind welcome!

npm install current-device js 端判断程序运行的设备的更多相关文章

  1. NPM install -save 和 -save-dev 傻傻分不清

    本文原文地址:https://www.limitcode.com/detail/59a15b1a69e95702e0780249.html 回顾 npm install 命令 最近在写Node程序的时 ...

  2. 下载一个vue项目执行npm install 后运行项目npm run dev后出错 - 问题解决

    在SVN上拉下来一个vue项目,上面没有提交项目里面的node_modules文件夹,所以要自己执行 npm install 安装,但安装完后运行项目后却报错了: $ npm run dev > ...

  3. 运行npm install命令的时候会发生什么?

    摘要:我们日常在下载第三方依赖的时候,都会用到一个命令npm install,那么你知道,在运行这个命令的时候都会发生什么吗? 本文分享自华为云社区<运行npm install命令的时候会发生什 ...

  4. This usually happens because your environment has changed since running `npm install`

    此时运行按照提示执行  npm rebuild node-sass  命令,(如若不行,则先运行npm install node-sass命令执行) 然后再运行 node命令,启动服务.

  5. 通过JS语句判断WEB网站的访问端是电脑还是手机

    通过JS语句判断WEB网站的访问端是电脑还是手机,以显示不同的页面! 目录腾讯网的适配代码如何判断访问网站的机器类型-如何判断ipadJS 判断浏览器客户端类型(ipad,iphone,android ...

  6. js如何判断用户是在pc端和还是移动端访问

    js如何判断用户是在pc端和还是移动端访问 来源:A5技术交流 作者:wofa 时间:2014-04-25收藏本页 最近一直在忙我们团队的项目“咖啡之翼”,在这个项目中,我们为移动平台提供了一个优秀的 ...

  7. NW.js构建PC收银端安装程序的指南

    1.首先下载nw.js的SDK: https://nwjs.org.cn/download.html 2.SDK目录下新建myapp文件夹: 3.myapp文件夹内新建package.json文件: ...

  8. [Node.js] npm init && npm install

    npm init: For create package.json file which will recode the dependence. npm install: You can also w ...

  9. ☀【Grunt】package.json, Gruntfile.js, npm install, grunt

    npm install --registry http://registry.npm.taobao.org/ 切换源 Grunt.js 在前端项目中的实战http://beiyuu.com/grunt ...

  10. $ npm install opencv ? 你试试?! 在windows环境下,使用node.js调用opencv攻略

    博主之前写过一篇文章<html5与EmguCV前后端实现——人脸识别篇>,叙述的是opencv和C#的故事.最近在公司服务器上更新了一套nodejs环境,早就听闻npm上有opencv模块 ...

随机推荐

  1. CentOS 8 安装 oracle 23c CentOS9 Error deal

    1.环境准备 软件准备 序号 软件 下载地址 1 VirtualBox https://www.virtualbox.org/wiki/Downloads 2 CentOS Stream 8 http ...

  2. 重新点亮linux 命令树————查看进程[二十一]

    前言 简单介绍一下进程. 正文 进程管理: 进程的概念与进程查看 进程的控制命令 进程的通信方式---信号 守护进程和系统日志 服务管理工具 systemctl SELinux 简介 进程概念: ht ...

  3. 前端之多线程 ---webworker

    一.啥是workerJavaScript为单线程,worker则为JavaScript创建多线程环境.使用场景如:计算文件hash,计算大于1G的文件hash过程是很慢的,但由于要将hash传给后端, ...

  4. 物联网浏览器(IoTBrowser)-使用深度学习开发防浸水远程报警

    一.起因 新房子买在2楼,反水概率较大,加上无良开发商的劣质材料,就我所在楼栋已经发生几起反水事件,而且是高层反水,有几户重复出现反水,原因是管道中间有一个钢筋 :( 二.解决方案 1.水浸传感器+W ...

  5. 力扣1045(MySQL)-买下所有产品的客户(中等)

    题目: Customer 表: Product 表: 写一条 SQL 查询语句,从 Customer 表中查询购买了 Product 表中所有产品的客户的 id. 示例:  解题思路: 建表语句: 1 ...

  6. 阿里云 ACK 容器服务生产级可观测体系建设实践

    简介: 随着容器被越来越对企业接纳与落地,可观测成为重点.那么,让我们深入了解阿里云 ACK 容器服务生产级可观测体系建设实践,为自身业务可观测提供参考- 作者:冯诗淳(行疾)   ACK 可观测体系 ...

  7. 殷浩详解DDD:领域层设计规范

    简介: 在一个DDD架构设计中,领域层的设计合理性会直接影响整个架构的代码结构以及应用层.基础设施层的设计.但是领域层设计又是有挑战的任务,特别是在一个业务逻辑相对复杂应用中,每一个业务规则是应该放在 ...

  8. dotnet core 3.1 将 UWP 控件嵌入到 WPF 应用 收到 UIA 消息主线程卡住

    本文记录一个问题,此问题是在 .NET Core 3.1 的 WPF 应用里面,嵌入 UWP 控件之后,在收到 UIA 的消息时,可能让主线程卡住.暂时此问题还不知道具体的复现步骤,此问题预计和 WP ...

  9. WPF 修复引用库报错 所使用的 PresentationFramework 6

    本文记录一个 WPF 构建的坑,这是 WPF 仓库的运维管理大意挖的坑.将会在大家使用低版本的 dotnet 6 如 6.0.1 版本时,引用其他人使用高 dotnet 6 版本,如 dotnet 6 ...

  10. WPF 通过 GifBitmapDecoder 调用 WIC 解析 Gif 和进行动画播放的简单方法

    本文告诉大家如何在 WPF 里,通过 GifBitmapDecoder 调用 WIC 层来解析 GIF 图片,然后采用动画的方式进行播放 在上一篇博客告诉大家,可以通过 GifBitmapDecode ...