当前位置:   article > 正文

shake.js监测手机摇一摇

shake.js

下面是shake.js代码【ios需要注意权限问题】

  1. (function(global, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. define(function() {
  4. return factory(global, global.document);
  5. });
  6. } else if (typeof module !== 'undefined' && module.exports) {
  7. module.exports = factory(global, global.document);
  8. } else {
  9. global.Shake = factory(global, global.document);
  10. }
  11. } (typeof window !== 'undefined' ? window : this, function (window, document) {
  12. 'use strict';
  13. function Shake(options) {
  14. //feature detect
  15. this.hasDeviceMotion = 'ondevicemotion' in window;
  16. this.options = {
  17. threshold: 15, //默认摇动阈值
  18. timeout: 1000 //默认两次事件间隔时间
  19. };
  20. if (typeof options === 'object') {
  21. for (var i in options) {
  22. if (options.hasOwnProperty(i)) {
  23. this.options[i] = options[i];
  24. }
  25. }
  26. }
  27. //使用date防止重复调用
  28. this.lastTime = new Date();
  29. //accelerometer values
  30. this.lastX = null;
  31. this.lastY = null;
  32. this.lastZ = null;
  33. //创建自定义事件
  34. if (typeof document.CustomEvent === 'function') {
  35. this.event = new document.CustomEvent('shake', {
  36. bubbles: true,
  37. cancelable: true
  38. });
  39. } else if (typeof document.createEvent === 'function') {
  40. this.event = document.createEvent('Event');
  41. this.event.initEvent('shake', true, true);
  42. } else {
  43. return false;
  44. }
  45. }
  46. //重置时间计时器
  47. Shake.prototype.reset = function () {
  48. this.lastTime = new Date();
  49. this.lastX = null;
  50. this.lastY = null;
  51. this.lastZ = null;
  52. };
  53. //开始监听 devicemotion
  54. Shake.prototype.start = function () {
  55. this.reset();
  56. if (this.hasDeviceMotion) {
  57. window.addEventListener('devicemotion', this, false);
  58. }
  59. };
  60. //停止监听 devicemotion
  61. Shake.prototype.stop = function () {
  62. if (this.hasDeviceMotion) {
  63. window.removeEventListener('devicemotion', this, false);
  64. }
  65. this.reset();
  66. };
  67. //计算是否触发摇动
  68. Shake.prototype.devicemotion = function (e) {
  69. var current = e.accelerationIncludingGravity;
  70. var currentTime;
  71. var timeDifference;
  72. var deltaX = 0;
  73. var deltaY = 0;
  74. var deltaZ = 0;
  75. if ((this.lastX === null) && (this.lastY === null) && (this.lastZ === null)) {
  76. this.lastX = current.x;
  77. this.lastY = current.y;
  78. this.lastZ = current.z;
  79. return;
  80. }
  81. deltaX = Math.abs(this.lastX - current.x);
  82. deltaY = Math.abs(this.lastY - current.y);
  83. deltaZ = Math.abs(this.lastZ - current.z);
  84. if (((deltaX > this.options.threshold) && (deltaY > this.options.threshold)) || ((deltaX > this.options.threshold) && (deltaZ > this.options.threshold)) || ((deltaY > this.options.threshold) && (deltaZ > this.options.threshold))) {
  85. //计算上次触发摇动到现在的间隔时间
  86. currentTime = new Date();
  87. timeDifference = currentTime.getTime() - this.lastTime.getTime();
  88. if (timeDifference > this.options.timeout) {
  89. window.dispatchEvent(this.event);
  90. this.lastTime = new Date();
  91. }
  92. }
  93. this.lastX = current.x;
  94. this.lastY = current.y;
  95. this.lastZ = current.z;
  96. };
  97. //事件处理
  98. Shake.prototype.handleEvent = function (e) {
  99. if (typeof (this[e.type]) === 'function') {
  100. return this[e.type](e);
  101. }
  102. };
  103. return Shake;
  104. }));

在页面中创建新实例进行调用

  1. //创建实例
  2. var myShakeEvent = new Shake({
  3. threshold: 10, // 摇动阈值
  4. timeout: 500 // 事件发生频率,是可选值
  5. });
  6. // 监听设备动作
  7. myShakeEvent.start();
  8. //添加一个监听事件
  9. window.addEventListener('shake', shakeEventDidOccur, false);
  10. //摇动回调函数
  11. function shakeEventDidOccur() {
  12. alert('摇了一次');
  13. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/97152
推荐阅读
相关标签
  

闽ICP备14008679号