30秒学会 JavaScript 片段 · 2018年10月27日

30秒学会 JavaScript 片段 – supportsTouchEvents

Returns true if touch events are supported, false otherwise.

Check if ontouchstart exists in window or window.DocumentTouch is true and the current document is an instance of it.

代码片段

const supportsTouchEvents = () =>
  window &&
  ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);

使用样例

supportsTouchEvents(); // true