如何访问conditionizr对象?

时间:2013-05-26 19:40:07

标签: javascript touch browser-detection

我需要知道用户是否在我的JavaScript代码中使用移动触摸设备来注册任何悬停eventHandlers。因为我不想像以下那样使用userAgent嗅探:

isMobile = function() {
  return /iPhone|iPod|iPad|Android|BlackBerry/.test(navigator.userAgent);
};

我期望conditionizr给我一些类似conditionizr.isTouch的东西。我想出的解决方案是测试这样的触摸类:

isMobile = function() {
  return $('#conditionizr').hasClass('touch');
};

这是有效的,因为conditionizr将类触摸添加到html

<html id="conditionizr" class=" chrome no-retina no-touch mac">

但我想有更好的方法来访问设置吗?

2 个答案:

答案 0 :(得分:2)

设置和内部变量未公开,但您只需使用相同的检查条件:

isMobile = function() {
  return ('ontouchstart' in window)
}

答案 1 :(得分:2)

Conditionizr v4版本更新

新的Conditionizr API完全符合您的要求:

if (conditionizr.touch) {
    // touch
}

......以及许多其他出色的增强功能,例如.on()API:

conditionizr.on('chrome', function () {
    // callback for Chrome
});

还有更多!

http://conditionizr.com