有没有办法用Javascript检测Force Click?

时间:2015-11-03 18:38:40

标签: javascript macos browser click pressure.js

有没有办法用Javascript检测强制点击?我知道有很多方法可以检测常规和右键点击,但是Force Clicks呢?

2 个答案:

答案 0 :(得分:3)

我找到了一个名为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。如果您希望它在两个简单的上运行,请删除该选项。

答案 1 :(得分:2)

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

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

来源:WebKit DOM Programming Documentation