简体   繁体   English

确定用户是否在移动设备上的最简单方法

[英]Easiest way to determine if user is on mobile device

I'm showing a notification bar on my website, and frankly, it doesn't work well when its on a mobile device.我在我的网站上显示了一个通知栏,坦率地说,当它在移动设备上时效果不佳。 I'd like to show the bar ONLY for desktop users.我只想为桌面用户显示该栏。

What is the easiest way to determine if a user is on desktop or on mobile?确定用户是使用台式机还是移动设备的最简单方法是什么?

Check this http://detectmobilebrowsers.com/检查这个http://detectmobilebrowsers.com/

Work for Javascript, jQuery etc.为 Javascript、jQuery 等工作。

A user agent check is the "easiest", though you could easily employ CSS3 media queries用户代理检查是“最简单的”,尽管您可以轻松使用CSS3 媒体查询

Here is an example that checks iphone, android and blackberry;下面是一个检查 iphone、android 和 blackberry 的例子; you could easily add other mobile browsers.您可以轻松添加其他移动浏览器。

var is_mobile = !!navigator.userAgent.match(/iphone|android|blackberry/ig) || false;

I find that it's best to use feature detection.我发现最好使用特征检测。 Use Modernizr to detect if it's a touch device.使用Modernizr检测它是否是触摸设备。 You can do things like:您可以执行以下操作:

var mousedown = 'mousedown';

if (Modernizr.touch) {
    mousedown = 'touchstart';
}

$('.foo').on(mousedown, handleMouseDown);

And then use CSS Media Queries for handling screen width (and it's also easy to detect screen width with javascript).然后使用 CSS Media Queries 处理屏幕宽度(使用 javascript 检测屏幕宽度也很容易)。 That way you can correctly handle touch devices with large screens, or non-touch devices with small screens.这样您就可以正确处理大屏幕的触摸设备,或小屏幕的非触摸设备。

If you use modernizr .如果您使用Modernizr a "no-touch" class will be added to the element.一个“非接触”类将被添加到元素中。 You could hide the bar by default and add a css rule to show the bar if the "no-touch" class exists.如果存在“no-touch”类,您可以默认隐藏栏并添加 css 规则以显示栏。 Example:例子:

default:默认:

.bar{display:none;}

desktop:桌面:

.no-touch .bar{display:block;}

If the user is on a mobile device this javascript 'if' will return true.如果用户在移动设备上,此 javascript 'if' 将返回 true。

if (navigator.userAgent.indexOf('Mobile') !== -1) { ... if (navigator.userAgent.indexOf('Mobile') !== -1) { ...

See also: https://deviceatlas.com/blog/list-of-user-agent-strings另请参阅: https : //deviceatlas.com/blog/list-of-user-agent-strings

The easiest way to differentiate between touch and non-touch devices is using media queries.区分触摸设备和非触摸设备的最简单方法是使用媒体查询。

1) CSS to target Mobile/Touch Devices can be written using media query, 1) 可以使用媒体查询编写针对移动/触摸设备的 CSS,

  @media (hover: none), (pointer: coarse) {}

2) CSS to target Desktop/Non-Touch Devices (only) can be written using media query, 2) 可以使用媒体查询编写针对桌面/非触摸设备(仅限)的 CSS,

  @media not all and (pointer: coarse) {}

On Few latest mobile devices (Eg: IOS 10+, one plus etc.,.) hover is detected hence we use, the (2) to identify non-touch devices.在少数最新的移动设备(例如:IOS 10+、一加等)上检测到悬停,因此我们使用 (2) 来识别非触摸设备。

 const is_mobile = navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/BlackBerry/i);

if (is_mobile != null){
    popup.modal('show');
}

If you are reading this post 2021, there is an even easier way to find this out.如果您正在阅读 2021 年的这篇文章,那么还有一种更简单的方法可以找到它。

let isMobile = window.navigator.userAgentData.mobile;
console.log(isMobile);

The above method simply returns a boolean value.上述方法只返回一个 boolean 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM