iOS 3D Touch API 使用 Javascript 网络应用程序

iOS 3D Touch API using Javascript for Web App

iPhone 6s 具有适用于本机应用程序的 3D 触控 API。我想知道 Apple 是否已从 Javascript 为网络应用程序启用 3D 触摸访问?

就像我们使用标准触摸事件一样

element.addEventListener("touchstart", handleStart, false);

Safari 9 支持桌面版。请参阅 Apple 的 WebKit DOM 编程主题中的 Responding to Force Touch Events from JavaScript

不过,我在 iOS 上找不到任何关于在 Safari 9 中使用它的文档。

您可以在 iPhone 和 Mac 上使用强制触摸。你可以使用JS库Pressure.js。如果您有 iPhone 6s 或带有新 Force Touch 触控板的新书 Mac,那么在 iOS 和 OSX 上获取 Force 和 3D Touch 值真的很容易。目前仅支持 Safari,但希望很快会有更多浏览器支持。

语法也很简单,举个例子:

Pressure.set('#element', {
  start: function(){
    // this is called on force start
  },
  end: function(){
    // this is called on force end
  },
  startDeepPress: function(){
    // 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
    // here the 'force' variable passed in container the current pressure force
  },
  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
  }
});