touchevents.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*!
  2. {
  3. "name": "Touch Events",
  4. "property": "touchevents",
  5. "caniuse": "touch",
  6. "tags": ["media", "attribute"],
  7. "notes": [{
  8. "name": "Touch Events spec",
  9. "href": "https://www.w3.org/TR/2013/WD-touch-events-20130124/"
  10. }],
  11. "warnings": [
  12. "Indicates if the browser supports the Touch Events spec, and does not necessarily reflect a touchscreen device"
  13. ],
  14. "knownBugs": [
  15. "False-positive on some configurations of Nokia N900",
  16. "False-positive on some BlackBerry 6.0 builds – https://github.com/Modernizr/Modernizr/issues/372#issuecomment-3112695"
  17. ]
  18. }
  19. !*/
  20. /* DOC
  21. Indicates if the browser supports the W3C Touch Events API.
  22. This *does not* necessarily reflect a touchscreen device:
  23. * Older touchscreen devices only emulate mouse events
  24. * Modern IE touch devices implement the Pointer Events API instead: use `Modernizr.pointerevents` to detect support for that
  25. * Some browsers & OS setups may enable touch APIs when no touchscreen is connected
  26. * Future browsers may implement other event models for touch interactions
  27. See this article: [You Can't Detect A Touchscreen](http://www.stucox.com/blog/you-cant-detect-a-touchscreen/).
  28. It's recommended to bind both mouse and touch/pointer events simultaneously – see [this HTML5 Rocks tutorial](https://www.html5rocks.com/en/mobile/touchandmouse/).
  29. This test will also return `true` for Firefox 4 Multitouch support.
  30. */
  31. define(['Modernizr', 'prefixes', 'mq'], function(Modernizr, prefixes, mq) {
  32. // Chrome (desktop) used to lie about its support on this, but that has since been rectified: https://bugs.chromium.org/p/chromium/issues/detail?id=36415
  33. // Chrome also changed its behaviour since v70 and recommends the TouchEvent object for detection: https://www.chromestatus.com/feature/4764225348042752
  34. Modernizr.addTest('touchevents', function() {
  35. if (('ontouchstart' in window) || window.TouchEvent || window.DocumentTouch && document instanceof DocumentTouch) {
  36. return true;
  37. }
  38. // include the 'heartz' as a way to have a non matching MQ to help terminate the join
  39. // https://github.com/Modernizr/Modernizr/issues/1814
  40. var query = ['(', prefixes.join('touch-enabled),('), 'heartz', ')'].join('');
  41. return mq(query);
  42. });
  43. });