我正在寻找一个函数,返回一个布尔值,如果用户正在使用移动浏览器与否。

我知道我可以使用导航器。userAgent并使用regex编写该函数,但是用户代理对于不同的平台来说太复杂了。我怀疑匹配所有可能的设备是否容易,我认为这个问题已经解决了很多次,所以应该有某种完整的解决方案来完成这样的任务。

我正在看这个网站,但不幸的是,脚本是如此神秘,我不知道如何使用它为我的目的,这是创建一个返回true/false的函数。


当前回答

使用window.screen怎么样?宽度”?

if (window.screen.width < 800) {
// do something
}

or

if($(window).width() < 800) {
//do something
}

我想这是最好的方法,因为每天都有新的移动设备!

(虽然我认为旧的浏览器不支持它,但试试看吧:))

其他回答

如何:

if (typeof screen.orientation !== 'undefined') { ... }

...因为智能手机通常支持这个属性,而桌面浏览器不支持。在MDN中见。

编辑1:正如@Gajus指出的,窗口。Orientation现在已弃用,不应该使用。

编辑2:您可以使用实验屏幕。Orientation而不是弃用的window.orientation。在MDN中见。

编辑3:从窗口更改。朝向屏幕。朝向

const isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

如何使用

if( isMobile.any() ) alert('Mobile');

要查看用户是否在特定的移动设备上:

if( isMobile.iOS() ) alert('iOS');

裁判:http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript

github上的增强版:https://github.com/smali-kazmi/detect-mobile-browser

这取决于用例。所有的移动设备都需要电池。如果你想要的是在不耗尽电池的情况下计算电量,使用电池状态API:

navigator.getBattery().then(battery => {
  battery.charging ? 'charging' : 'not charging';
});

如果您正在寻找的是表示的使用matchMedia,它返回一个布尔值:

if (window.matchMedia("(min-width: 400px)").matches) {
  /* the viewport is at least 400 pixels wide */
} else {
  /* the viewport is less than 400 pixels wide */
}

或者将它们结合起来,在平板设备上获得更好的用户体验。

啊,是的,这个古老的问题……

这取决于你对知识的反应。

1. 你想改变UI,让它适合不同的屏幕尺寸吗?

使用媒体查询。

2. 你想显示/隐藏东西或改变基于鼠标和触摸的功能吗?

上面的答案可以解决问题,但也可能出现用户同时拥有两个选项并切换的情况。在这种情况下,当你检测到鼠标或触摸事件时,你可以切换一些JS变量和/或向文档主体添加一个类

  window.addEventListener("mousemove", function () {
    isTouch = false;
    document.body.classList.add("canHover");
  });
  window.addEventListener("touchstart", function () {
    isTouch = true;
    document.body.classList.remove("canHover");
  });
body.canHover #aButtonOrSomething:hover {
  //css attributes
}
  document
    .getElementById("aButtonOrSomething")
    .addEventListener("mouseover", showTooltip);
  document
    .getElementById("aButtonOrSomething")
    .addEventListener("click", function () {
      if (isTouch) showTooltip();
    });

3.你想做一些具体的事情,知道他们有什么设备吗?

使用公认的答案。

特征检测

const isMobile = localStorage.mobile || window.navigator.maxTouchPoints > 1;

截至2022-02-07,工作在CHROME + SAFARI::结合功能检测并尝试本线程和其他网站中的所有内容。localStorage。移动工作在Chrome移动;后者适用于Safari移动版。在打开或不打开dev-tools时和/或在移动模拟器上不会触发桌面浏览器。在撰写本文时,它会触发一个真正的移动浏览器,而不是桌面。

请考虑

我还在Win10上的联想X1 Yoga(键盘模式或平板模式)上进行了测试

localStorage。无论如何,移动是没有定义的 当笔记本电脑处于键盘模式时:window.navigator。maxTouchPoints is 1→isMobile为false 当键盘向后翻转,笔记本电脑处于平板电脑模式时:window.navigator。maxTouchPoints是10→isMobile是true