TouchEvent#touches的文档记录范围是什么?是否存在0个触点场景?
TouchEvent.touches 的触点数量范围说明
触点数量的范围细节
最小数量(0的场景):
touches数组长度可以为0,但仅会出现在touchend事件中——当最后一个触点离开屏幕时,touches就会变成空数组。而在touchstart这类触点新增的事件里,touches的长度至少为1,因为事件触发的前提就是有触点开始接触屏幕。最大数量:没有统一的标准上限,完全由设备的硬件触摸支持能力决定。比如不少现代移动设备支持5个及以上触点,MDN文档里的示例只处理到3个触点,只是为了简化演示逻辑,并非API本身的限制。
原示例代码说明
原示例仅做演示用,实际开发可根据设备支持情况扩展:
someElement.addEventListener('touchstart', (e) => { // Invoke the appropriate handler depending on the // number of touch points. switch (e.touches.length) { case 1: handle_one_touch(e); break; case 2: handle_two_touches(e); break; case 3: handle_three_touches(e); break; default: console.log("Not supported"); break; } }, false);
内容的提问来源于stack exchange,提问作者Tristan F.-R.
相关产品推荐
相关产品推荐

