鼠标方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title></title>
  8. <style>
  9. .box {
  10. width: 100px;
  11. height: 100px;
  12. background-color: orange;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="box"> </div>
  18. <br><br><br><br><br><br><br><br><br>
  19. <br><br><br><br><br><br><br><br><br><br>
  20. <br><br><br><br><br><br><br><br><br><br>
  21. <br><br><br><br><br><br><br><br><br><br><br><br>
  22. <br><br><br><br><br><br><br><br><br>
  23. <div style="height: 1000px;"></div>
  24. <script>
  25. //鼠标行为->坐标系
  26. //clientX/Y 鼠标位置相对于当前可视区域的坐标
  27. // (不包含滚动条的距离)与滚动条状态无关
  28. //layerX/Y 同pageXY相同 IE11以下同clientX/Y
  29. //screenX/Y 鼠标位置相对于屏幕的坐标 整个屏幕
  30. //与浏览器的位置 大小 全屏 半屏等有关
  31. //x/y 同clientX/Y相同 FF不支持(火狐不支持)因为火狐浏览器不兼容所以最好别用
  32. //pageX/pageY ie9以下不支持
  33. //鼠标位置相当于当前文档的坐标(包含滚动条的距离)滚动条偏移的距离+clientX/Y
  34. document.onclick = function (e) {//e是事件对象
  35. var e = e || window.event;
  36. // console.log(e)
  37. // console.log(e.clientY, e.pageY);
  38. console.log(e.screenY,e.pageY);
  39. }
  40. </script>
  41. </body>
  42. </html>

offsetX/Y

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title></title>
  8. <style>
  9. .box {
  10. width: 100px;
  11. height: 100px;
  12. background-color: orange;
  13. }
  14. .box1{
  15. position: absolute;
  16. top: 200px;
  17. left: 200px;
  18. width: 100px;
  19. height: 100px;
  20. background-color: orange;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="box"> </div>
  26. <div class="box1"></div>
  27. <br><br><br><br><br><br><br><br><br>
  28. <br><br><br><br><br><br><br><br><br><br>
  29. <br><br><br><br><br><br><br><br><br><br>
  30. <br><br><br><br><br><br><br><br><br><br><br><br>
  31. <br><br><br><br><br><br><br><br><br>
  32. <div style="height: 1000px;"></div>
  33. <script>
  34. //鼠标行为->坐标系
  35. //clientX/Y 鼠标位置相对于当前可视区域的坐标
  36. // (不包含滚动条的距离)与滚动条状态无关
  37. //layerX/Y 同pageXY相同 IE11以下同clientX/Y
  38. //screenX/Y 鼠标位置相对于屏幕的坐标 整个屏幕
  39. //与浏览器的位置 大小 全屏 半屏等有关
  40. //x/y 同clientX/Y相同 FF不支持(火狐不支持)因为火狐浏览器不兼容所以最好别用
  41. //pageX/pageY ie9以下不支持
  42. //鼠标位置相当于当前文档的坐标(包含滚动条的距离)滚动条偏移的距离+clientX/Y
  43. //offsetX/Y 鼠标位置相对于块元素的坐标 与相对的块元素有关 与其他的无关
  44. //(包含边框,safari不包含边框)
  45. var box=document.getElementsByClassName('box')[0];
  46. var box1=document.getElementsByClassName('box1')[0];
  47. box.onclick = function (e) {//e是事件对象
  48. var e = e || window.event;
  49. // console.log(e)
  50. // console.log(e.clientY, e.pageY);
  51. console.log(e.offsetX,e.offsetY);
  52. }
  53. box1.onclick = function(e) {
  54. console.log(e.offsetX,e.offsetY);
  55. }
  56. </script>
  57. </body>
  58. </html>

兼容性

pagePos 整个页面,算上滚动条的获取鼠标方位

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title></title>
  8. <style>
  9. .box {
  10. width: 100px;
  11. height: 100px;
  12. background-color: orange;
  13. }
  14. .box1{
  15. position: absolute;
  16. top: 200px;
  17. left: 200px;
  18. width: 100px;
  19. height: 100px;
  20. background-color: orange;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="box"> </div>
  26. <div class="box1"></div>
  27. <br><br><br><br><br><br><br><br><br>
  28. <br><br><br><br><br><br><br><br><br><br>
  29. <br><br><br><br><br><br><br><br><br><br>
  30. <br><br><br><br><br><br><br><br><br><br><br><br>
  31. <br><br><br><br><br><br><br><br><br>
  32. <div style="height: 1000px;"></div>
  33. <script type="text/javascript" src="./utils.js"></script>
  34. <script>
  35. var box=document.getElementsByClassName('box')[0];
  36. document.onclick=function(e) {
  37. var e=e||window.event;
  38. console.log(e.pageY,pagePos(e).Y)
  39. }
  40. //整个页面,算上滚动条的获取鼠标方位
  41. function pagePos(e){
  42. var sLeft=getScrollOffset().left,
  43. sTop=getScrollOffset().top,
  44. cLeft=document.documentElement.clientLeft||0,
  45. cTop=document.documentElement.clientTop || 0;
  46. return {
  47. X:e.clientX+sLeft-cLeft,
  48. Y:e.clientY+sTop-cTop
  49. }
  50. }
  51. </script>
  52. </body>
  53. </html>

onmousemove

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title></title>
  8. </head>
  9. <style>
  10. .box {
  11. position: absolute;
  12. top: 200px;
  13. left: 200px;
  14. width: 100px;
  15. height: 100px;
  16. background-color: orange;
  17. }
  18. </style>
  19. <body>
  20. <div class="box"></div>
  21. <script type="text/javascript" src="./utils.js"></script>
  22. <script>
  23. var box = document.getElementsByClassName('box')[0];
  24. console.log(box)
  25. //mousedown mouseup mousemove
  26. // box.onmousedown=function(e) {
  27. // box.onmousemove=function(e) {
  28. // }
  29. // box.onmouseup=function(e) {
  30. // }
  31. // }
  32. document.onmousemove = function(e) {
  33. var e=e || window.event,
  34. page=pagePos(e);
  35. box.style.left=page.X+'px';
  36. box.style.top = page.Y + "px";
  37. }
  38. </script>
  39. </body>
  40. </html>

偏移问题

image.png

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title></title>
  8. </head>
  9. <style>
  10. .box {
  11. position: absolute;
  12. top: 200px;
  13. left: 200px;
  14. width: 100px;
  15. height: 100px;
  16. background-color: orange;
  17. }
  18. </style>
  19. <body>
  20. <div style="height: 1000px;"></div>
  21. <div class="box"></div>
  22. <script type="text/javascript" src="./utils.js"></script>
  23. <script>
  24. var box = document.getElementsByClassName('box')[0];
  25. console.log(box)
  26. //mousedown mouseup mousemove
  27. /*缺陷:点击中间等box区域,box也会直接移动,因为把box左上角赋予鼠标点击的方位,
  28. 有时只是想点击方块,可以移动box,之后再移动,而不是直接移动
  29. */
  30. // box.onmousedown=function(e) {
  31. // document.onmousemove=function(e) {
  32. // var e=e||window.event,
  33. // page=pagePos(e);
  34. // box.style.left=page.X + "px";
  35. // box.style.top = page.Y + "px";
  36. // }
  37. // box.onmouseup=function(e) {
  38. // document.onmousemove=null;
  39. // }
  40. // }
  41. box.onmousedown=function(e) {
  42. var e=e||window.event;
  43. x=pagePos(e).X-getStyles(box,'left');
  44. /*算上滚动条偏移的鼠标点击的坐标-算上滚动条box左上角的距离
  45. =鼠标点击的坐标至box左上角的距离*/
  46. y=pagePos(e).Y - getStyles(box, "top");
  47. console.log(x,y)
  48. document.onmousemove=function(e) {//冒泡事件
  49. var e=e||window.event,
  50. page=pagePos(e);
  51. box.style.left=page.X -x+ "px";
  52. box.style.top = page.Y-y + "px";
  53. }
  54. document.onmouseup=function(e) {
  55. // document.onmousemove=null;
  56. // document.onmouseup = null;
  57. this.onmousemove=null;
  58. this.onmouseup = null;
  59. }
  60. }
  61. // document.onmousemove = function(e) {
  62. // var e=e || window.event,
  63. // page=pagePos(e);
  64. // box.style.left=page.X+'px';
  65. // box.style.top = page.Y + "px";
  66. // }
  67. </script>
  68. </body>
  69. </html>

封装拖拽函数

但拖拽边界有问题,下一课程会有解决之道 得重写 满足不了需求 重新封装

  1. //拖拽 封装好 以后直接调用就行了
  2. //有边界
  3. function elemDrag(elem) {
  4. var x,
  5. y;
  6. addEvent(elem, 'mousedown', function (e) {
  7. // console.log('1');
  8. var e = e || window.event;
  9. // console.log(elem);
  10. x = pagePos(e).X - getStyles(elem, 'left');
  11. y = pagePos(e).Y - getStyles(elem, 'top');
  12. addEvent(document, 'mousemove', mouseMove);
  13. addEvent(document, 'mouseup', mouseUp);
  14. cancelBubble(e);//保险起见 把阻止事件放进去
  15. preventDefaultEvent(e);
  16. });
  17. function mouseMove(e) {
  18. console.log(elem);
  19. var e = e || window.event;
  20. elem.style.top = pagePos(e).Y - y + 'px';
  21. elem.style.left = pagePos(e).X - x + 'px';
  22. }
  23. function mouseUp(e) {
  24. var e = e || window.event;
  25. removeEvent(document, 'mousemove', mouseMove);
  26. removeEvent(document, 'mouseup', mouseUp);
  27. }
  28. }