有什么方法可以用 Javascript 检测强制点击吗?

Ral*_*thy 2 javascript browser macos click pressure.js

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

Ral*_*thy 6

我发现了一个名为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'});
Run Code Online (Sandbox Code Playgroud)

最后的选项{only: 'force'}确保它仅在 Mac 上运行强制触摸,而不在 iOS 上运行。如果您希望它在两个设备上运行,只需删除该选项即可。