繁体   English   中英

有什么方法可以使用JavaScript检测“强制点击”吗?

[英]Any way to detect Force Click with Javascript?

有什么方法可以使用Java技术检测强制点击? 我知道有多种方法可以检测常规点击和右键点击,但是强制点击呢?

我找到了一个名为Pressure.js的JS库,该库使处理Force / 3D Touch更加简单。 它适用于具有3D触摸的iOS和具有强制触摸的OSX。 如果您仅在Mac上将其用于强制触摸,则可以这样使用:

Pressure.set('#element', {
  start: function(event){
    // this is called on force start
  },
  end: function(){
    // this is called on force end
  },
  startDeepPress: function(event){
    // this is called on "force click" / "deep press", aka once the force is greater than 0.5
  },
  endDeepPress: function(){
    // this is called when the "force click" / "deep press" end
  },
  change: function(force, event){
    // this is called every time there is a change in pressure
  },
  unsupported: function(){
    // this is called once there is a touch on the element and the device or browser does not support Force or 3D touch
  }
}, {only: 'force'});

最后的{only: 'force'}选项可确保它仅在Mac上为强制触摸而运行,而在iOS上不运行。 如果您希望它同时运行,则删除该选项。

侦听webkitmouseforcewillbeginwebkitmouseforcedown事件。 您可以从传递给事件处理程序的对象中的webkitForce属性获取强制级别。

当然,这仅适用于具有Force Touch支持的Mac上的Safari。

来源: WebKit DOM编程文档

暂无
暂无

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

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