image.png

    事件: devicemotion

    image.pngimage.png
    image.png

    image.png

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <div>
    11. <p id="p1"></p>
    12. <p id="p2"></p>
    13. <p id="p3"></p>
    14. </div>
    15. </body>
    16. <script>
    17. function $my(id) {
    18. return document.getElementById(id);
    19. }
    20. var p1 = $my('p1');
    21. var p2 = $my('p2');
    22. var p3 = $my('p3');
    23. // 整个窗口的重力感应时间,此事件只能窗口调
    24. window.addEventListener('devicemotion', (e)=>{
    25. console.log(e);
    26. var motion = e.accelerationIncludingGravity;
    27. var x = Math.round(motion.x);
    28. var y = Math.round(motion.y);
    29. var z = Math.round(motion.z);
    30. if (!getAdr()) {
    31. x = -x;
    32. y = -y;
    33. z = -z;
    34. }
    35. p1.innerHTML = 'x: ' + x;
    36. p2.innerHTML = 'y: ' + y;
    37. p3.innerHTML = 'z: ' + z;
    38. // g = 10
    39. function getAdr(){
    40. var type = navigator.userAgent;
    41. var isAndroid = type.indexOf('Android') > -1 ||
    42. type.indexOf('Adr') > -1;
    43. return isAndroid;
    44. }
    45. });
    46. </script>
    47. </html>

    摇一摇:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <style>
    9. #con{
    10. width: 200px;
    11. height: 200px;
    12. line-height: 100px;
    13. text-align: center;
    14. font-size: 48px;
    15. color: black;
    16. position: absolute;
    17. top: 100px;
    18. left: 100px;
    19. background: yellowgreen;
    20. }
    21. </style>
    22. </head>
    23. <body>
    24. <div id="con">
    25. <p id="text">快来摇我</p>
    26. </div>
    27. </body>
    28. <script>
    29. function $my(id){
    30. return document.getElementById(id);
    31. }
    32. var lastX = 0;
    33. var lastY = 0;
    34. var lastZ = 0;
    35. // 定义幅度
    36. var maxRange = 70;
    37. var minRange = 10;
    38. // 节流阀
    39. var isRange = false;
    40. var con = $my('con');
    41. var text = $my('text');
    42. window.addEventListener('divicemotion', (e)=>{
    43. // 包括重力加速度
    44. var motion = e.accelerationIncludingGravity;
    45. var x = Math.round(motion.x);
    46. var y = Math.round(motion.y);
    47. var z = Math.round(motion.z);
    48. //取绝对值
    49. var distance = Math.abs(x - lastX) + Math.abs(y - lastY) + Math.abs(z - lastZ);
    50. if (distance > maxRange) {
    51. text.innerHTML = "摇一摇";
    52. isRange = true;
    53. }
    54. // 动作慢下来之后
    55. if (distance < minRange && isRange) {
    56. // 动作慢下来或停止
    57. setTimeout(() => {
    58. text.innerHTML = "触发事件";
    59. }, 2000);
    60. }
    61. lastX = x;
    62. lastY = y;
    63. lastY = z;
    64. });
    65. </script>
    66. </html>

    image.png

    1. (function(window){
    2. window.gesture = function(ele, callback){
    3. var isStart = false;
    4. // 判断用户是否触发了触摸事件,两个手指在元素上
    5. ele.addEventListener('touchstart', function(event){
    6. if(event.touches.length >= 2) {
    7. isStart = true;
    8. // 初始距离
    9. this.startDistance = getDistance(event.touches[0],event.touches[1]);
    10. // 初始角度
    11. this.startDeg = getDeg(event.touches[0],event.touches[1]);
    12. // 判断是否传入回调,调用函数
    13. if (callback && typeof callback['start'] === 'function'){
    14. callback['start']();
    15. }
    16. }
    17. });
    18. // 是否移动
    19. ele.addEventListener('touchmove', (event)=>{
    20. if(event.touches.length >= 2) {
    21. isStart = true;
    22. // 实时距离
    23. const currDistance = getDistance(event.touches[0],event.touches[1]);
    24. // 实时角度
    25. const currDeg = getDeg(event.touches[0],event.touches[1]);
    26. // 实时距离与初识距离的比例
    27. event.scale = currDistance / this.startDistance;
    28. // 计算实时角度和初识角度的差值
    29. event.rotation = currDeg - this.startDeg;
    30. // 判断是否传入回调,调用函数
    31. if (callback && typeof callback['change'] === 'function'){
    32. callback['change']();
    33. }
    34. }
    35. });
    36. ele.addEventListener('touchend', (event)=>{
    37. if(event.touches.length < 2 && isStart) {
    38. // 判断是否传入回调,调用函数
    39. if (callback && typeof callback['end'] === 'function'){
    40. callback['end']();
    41. }
    42. // 重置isStart
    43. isStart = false;
    44. }
    45. });
    46. // 获取两点间的距离
    47. function getDistance(touch1, touch2){
    48. const a = touch1.clientX - touch2.clientX;
    49. const b = touch1.clientY - touch2.clientY;
    50. return Math.sqrt(a*a + b*b);
    51. }
    52. // 获取两点间的角度
    53. function getDeg(touch1, touch2){
    54. const x = touch1.clientX - touch2.clientX;
    55. const y = touch1.clientY - touch2.clientY;
    56. // tan = 对边/临边
    57. var radian = Math.atan2(y, x);
    58. return radian * 180/Math.PI;
    59. }
    60. }
    61. })(window);
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <style>
    9. #con{
    10. width: 300px;
    11. height: 300px;
    12. text-align: center;
    13. font-size: 48px;
    14. color: black;
    15. position: absolute;
    16. top: 50px;
    17. left: 50px;
    18. background: greenyellow;
    19. }
    20. </style>
    21. <script src="./android.js"></script>
    22. </head>
    23. <body>
    24. <div id="con">
    25. <p id="p1">1</p>
    26. <p id="p2">2</p>
    27. <p id="p3">3</p>
    28. </div>
    29. <script>
    30. var con = document.getElementById('con');
    31. var p1 = document.getElementById("p1");
    32. var p2 = document.getElementById("p2");
    33. var p3 = document.getElementById("p3");
    34. gesture(con, {
    35. start: function(e){
    36. p1.innerHTML = '多指事件';
    37. },
    38. change: function(e){
    39. p2.innerHTML = '多指移动了';
    40. },
    41. end: function(e){
    42. p3.innerHTML = '手指抬起了';
    43. }
    44. });
    45. </script>
    46. </body>
    47. </html>