iOS 3D Touch API使用Javascript的Web应用程序

iOS 3D Touch API using Javascript for Web App

本文关键字:Web 应用程序 Javascript 使用 3D Touch API iOS      更新时间:2023-09-26

iphone 6s有原生应用的3D触摸API。我想知道苹果是否已经为web应用程序启用了Javascript的3D触摸访问?

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

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

Safari 9支持桌面。参见Apple的WebKit DOM编程主题中的响应JavaScript强制触摸事件。

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

你可以在iPhone和Mac上使用强制触摸。你可以使用JS库Pressure.js。如果你使用的是带有Force Touch触控板的iPhone 6s或新款Macbook,那么在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
  }
});