如何确定浏览器是否支持事件对象的属性? [英] How to determine if browser supports properties on event object?

查看:154
本文介绍了如何确定浏览器是否支持事件对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些支持触摸功能的浏览器(例如Mobile Safari)在<标尺旋转属性中具有 event 对象用于诸如 touchmove 之类的事件。

Some touch-enabled browsers (such as Mobile Safari) have a scale and rotation property available on their event object for events such as touchmove.

我可以像这样检测对 scale 属性的支持...

I can detect support for the scale property like so...

document.body.addEventListener("touchmove", function(event) {
    var supportsScaleProperty = !!event.scale;
});

但是,有没有一种方法可以检测到它而不必绑定侦听器,然后查找该属性

However, is there a way to detect it without having to bind a listener and then look for the property in the callback?

例如,是否可行?

var supportsScaleProperty = !!(new CustomEvent("TouchEvents")).scale;

我尝试查看 createEvent() ,但已弃用。我查看了 new CustomEvent(),但不确定用于触摸事件的字符串。

I tried looking at createEvent(), but it's deprecated. I looked at new CustomEvent(), but wasn't sure which string to use for touch events.

推荐答案

这似乎不可能。 TJ的解决方案不会告诉我属性是否存在(即使在支持它们的设备上也不存在)。

This doesn't seem to be possible. TJ's solution doesn't tell me if the properties exist (they're never present, even on devices which support them).

所以,似乎我被困住了...

So, it looks like I'm stuck with...

document.body.addEventListener("touchmove", function(event) {
    if (event.scale) {
         // ...
    }
});

这篇关于如何确定浏览器是否支持事件对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆