值
一个 float 值,表示用户对触摸表面施加的压力大小。该值介于 0.0(无压力)和 1.0(硬件能识别的最大压力)之间。如果不知道该值(例如触摸设备不支持此属性),则返回 0.0。在已知压力的环境中,force 属性所代表的绝对压力以及压力级别的灵敏度可能会有所不同。
示例
此示例演示了如何使用 Touch 接口的 Touch.force 属性。此属性是施加压力的相对值,范围在 0.0 到 1.0 之间,其中 0.0 表示无压力,1.0 表示触摸设备能够感应到的最高压力级别。
在以下代码片段中,touchstart 事件处理程序会遍历 targetTouches 列表并记录每个触摸点的 force 值,但代码可以根据该值调用不同的处理方式。
js
someElement.addEventListener("touchstart", (e) => {
// Iterate through the list of touch points and log each touch
// point's force.
for (let i = 0; i < e.targetTouches.length; i++) {
// Add code to "switch" based on the force value. For example
// minimum pressure vs. maximum pressure could result in
// different handling of the user's input.
console.log(`targetTouches[${i}].force = ${e.targetTouches[i].force}`);
}
});
规范
| 规范 |
|---|
| 触摸事件 # dom-touch-force |
浏览器兼容性
加载中…