image.png
计算从el元素左上角至鼠标单击元素的坐标的距离,x偏移,y偏移

初步封装

  1. // var dragNclick = function (a, b) {
  2. // console.log(a, b);
  3. // }; //不能写()
  4. // dragNclick(1, 2); //这是一个模块
  5. //拖拽 封装好 以后直接调用就行了
  6. //初步封装 把上节课的封装好
  7. var dragNclick = (function (elem) {
  8. drag();
  9. function drag() {
  10. var x, y;
  11. addEvent(elem, "mousedown", function (e) {
  12. // console.log('1');
  13. var e = e || window.event;
  14. // console.log(elem);
  15. x = pagePos(e).X - getStyles(elem, "left");
  16. y = pagePos(e).Y - getStyles(elem, "top");
  17. addEvent(document, "mousemove", mouseMove);
  18. addEvent(document, "mouseup", mouseUp);
  19. cancelBubble(e); //保险起见 把阻止事件放进去
  20. preventDefaultEvent(e);
  21. });
  22. function mouseMove(e) {
  23. console.log(elem);
  24. var e = e || window.event;
  25. elem.style.left = pagePos(e).X - x + "px";
  26. elem.style.top = pagePos(e).Y - y + "px";
  27. }
  28. function mouseUp(e) {
  29. var e = e || window.event;
  30. removeEvent(document, "mousemove", mouseMove);
  31. removeEvent(document, "mouseup", mouseUp);
  32. }
  33. }
  34. });

调用

  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. body{
  11. margin: 0;
  12. }
  13. a{
  14. position: absolute;
  15. top: 0;
  16. left: 0;
  17. width: 100px;
  18. height: 100px;
  19. background-color: green;
  20. }
  21. </style>
  22. <body>
  23. <!-- <a href="http://www.baidu.com">百度</a> -->
  24. <a href="javascript:;">百度</a>
  25. <!-- <a href="javascript:void(0)"></a> -->
  26. <!-- <button>点击</button> -->
  27. <script type="text/javascript" src="./utils.js"></script>
  28. <script type="text/javascript" src="./index.js"></script>
  29. <script>
  30. // var oBtn=document.getElementsByTagName('button')[0];
  31. var oLink=document.getElementsByTagName('a')[0];
  32. // oMenu=document.getElementsByTagName('div')[0];
  33. dragNclick(oLink)
  34. oBtn.onclick = function() {
  35. console.log('click');
  36. }
  37. oBtn.onmousedown=function() {
  38. console.log('mousedown');
  39. }
  40. oBtn.onmouseup = function() {
  41. console.log('mouseup');
  42. }
  43. // mousedown+mouseup=click
  44. //https://aoxvpn.cc/?ch=0
  45. </script>
  46. </body>
  47. </html>

点击与拖拽1

有不好的地方,当符合条件时既有点击,又有拖拽

  1. var dragNclick = (function (elem,elemClick) {
  2. var bTime=0,
  3. eTime = 0;
  4. drag();
  5. function drag() {
  6. var x, y;
  7. addEvent(elem, "mousedown", function (e) {
  8. // console.log('1');
  9. var e = e || window.event;
  10. bTime=new Date().getTime();
  11. console.log('bTime',bTime);
  12. // console.log(elem);
  13. x = pagePos(e).X - getStyles(elem, "left");
  14. y = pagePos(e).Y - getStyles(elem, "top");
  15. addEvent(document, "mousemove", mouseMove);
  16. addEvent(document, "mouseup", mouseUp);
  17. cancelBubble(e); //保险起见 把阻止事件放进去
  18. preventDefaultEvent(e);
  19. });
  20. function mouseMove(e) {
  21. // console.log(elem);
  22. var e = e || window.event;
  23. elem.style.left = pagePos(e).X - x + "px";
  24. elem.style.top = pagePos(e).Y - y + "px";
  25. }
  26. function mouseUp(e) {
  27. var e = e || window.event;
  28. eTime=new Date().getTime();
  29. console.log('减法结果',eTime-bTime)
  30. console.log('eTime',eTime)
  31. if(eTime-bTime<100){//如果小于100就是点击事件,而不是拖动事件
  32. // console.log(1)//可以先通过打印判断,条件是否能执行,是否得到预期,
  33. elemClick();
  34. }
  35. removeEvent(document, "mousemove", mouseMove);
  36. removeEvent(document, "mouseup", mouseUp);
  37. }
  38. }
  39. });

点击与拖拽2

  1. var dragNclick = (function (elem, elemClick) {
  2. var bTime = 0,
  3. eTime = 0,
  4. oPos=[];
  5. drag();
  6. function drag() {
  7. var x, y;
  8. addEvent(elem, "mousedown", function (e) {
  9. // console.log('1');
  10. var e = e || window.event;
  11. bTime = new Date().getTime();
  12. // console.log("bTime", bTime);
  13. // console.log(elem);
  14. x = pagePos(e).X - getStyles(elem, "left");
  15. y = pagePos(e).Y - getStyles(elem, "top");
  16. addEvent(document, "mousemove", mouseMove);
  17. addEvent(document, "mouseup", mouseUp);
  18. cancelBubble(e); //保险起见 把阻止事件放进去
  19. preventDefaultEvent(e);
  20. });
  21. function mouseMove(e) {
  22. // console.log(elem);
  23. var e = e || window.event;
  24. elem.style.left = pagePos(e).X - x + "px";
  25. elem.style.top = pagePos(e).Y - y + "px";
  26. }
  27. function mouseUp(e) {
  28. var e = e || window.event;
  29. eTime = new Date().getTime();
  30. oPos=[getStyles(elem,'left'),getStyles(elem,'top')];
  31. // console.log("减法结果", eTime - bTime);
  32. // console.log("eTime", eTime);
  33. if (eTime - bTime < 100) {
  34. //如果小于100就是点击事件,而不是拖动事件
  35. // console.log(1)//可以先通过打印判断,条件是否能执行,是否得到预期,
  36. elem.style.left=oPos[0]+'px';
  37. elem.style.top=oPos[1]+'px';
  38. elemClick();
  39. }
  40. removeEvent(document, "mousemove", mouseMove);
  41. removeEvent(document, "mouseup", mouseUp);
  42. }
  43. }
  44. });

元素原型封装

  1. Element.prototype.dragNclick = (function (_self,elemClick) {
  2. var bTime = 0,
  3. eTime = 0,
  4. oPos=[];
  5. drag.call(this);//改变this指向,变成调用的this
  6. function drag() {
  7. var x, y,
  8. _self=this;
  9. // console.log('this',this)
  10. addEvent(this, "mousedown", function (e) {
  11. // console.log('1');
  12. var e = e || window.event;
  13. bTime = new Date().getTime();
  14. // console.log("bTime", bTime);
  15. // console.log(elem);
  16. x = pagePos(e).X - getStyles(_self, "left");
  17. y = pagePos(e).Y - getStyles(_self, "top");
  18. addEvent(document, "mousemove", mouseMove);
  19. addEvent(document, "mouseup", mouseUp);
  20. cancelBubble(e); //保险起见 把阻止事件放进去
  21. preventDefaultEvent(e);
  22. });
  23. function mouseMove(e) {
  24. // console.log(elem);
  25. var e = e || window.event;
  26. _self.style.left = pagePos(e).X - x + "px";
  27. _self.style.top = pagePos(e).Y - y + "px";
  28. }
  29. function mouseUp(e) {
  30. var e = e || window.event;
  31. eTime = new Date().getTime();
  32. oPos=[getStyles(_self,'left'),getStyles(_self,'top')];
  33. // console.log("减法结果", eTime - bTime);
  34. // console.log("eTime", eTime);
  35. if (eTime - bTime < 100) {
  36. //如果小于100就是点击事件,而不是拖动事件
  37. // console.log(1)//可以先通过打印判断,条件是否能执行,是否得到预期,
  38. _self.style.left=oPos[0]+'px';
  39. _self.style.top=oPos[1]+'px';
  40. elemClick();
  41. }
  42. removeEvent(document, "mousemove", mouseMove);
  43. removeEvent(document, "mouseup", mouseUp);
  44. }
  45. }
  46. });

解决拖拽超出滚动条的问题

image.png

  1. Element.prototype.dragNclick = (function (_self,elemClick) {
  2. var bTime = 0,
  3. eTime = 0,
  4. oPos=[],
  5. wWidth=getViewportSize().width,
  6. wHeight=getViewportSize().height,
  7. eleWidth=getStyles(this,'width'),
  8. eleHeight=getStyles(this,'height');
  9. drag.call(this);//改变this指向,变成调用的this
  10. function drag() {
  11. var x, y,
  12. _self=this;
  13. // console.log('this',this)
  14. addEvent(this, "mousedown", function (e) {
  15. // console.log('1');
  16. var e = e || window.event;
  17. bTime = new Date().getTime();
  18. // console.log("bTime", bTime);
  19. // console.log(elem);
  20. x = pagePos(e).X - getStyles(_self, "left");
  21. y = pagePos(e).Y - getStyles(_self, "top");
  22. addEvent(document, "mousemove", mouseMove);
  23. addEvent(document, "mouseup", mouseUp);
  24. cancelBubble(e); //保险起见 把阻止事件放进去
  25. preventDefaultEvent(e);
  26. });
  27. function mouseMove(e) {
  28. // console.log(elem);
  29. var e = e || window.event,
  30. eleLeft=pagePos(e).X-x,//
  31. eleTop=pagePos(e).Y-y;
  32. if(eleLeft<=0){
  33. eleLeft=0;
  34. }else if(eleLeft>=wWidth-eleWidth){//
  35. eleLeft=wWidth-eleWidth-1;
  36. }
  37. if(eleTop<=0){
  38. eleTop=0;
  39. }else if(eleTop>=wHeight-eleHeight){
  40. eleTop=wHeight-eleHeight-1;
  41. }
  42. // _self.style.left = pagePos(e).X - x + "px";
  43. // _self.style.top = pagePos(e).Y - y + "px";
  44. _self.style.left = eleLeft+ "px";
  45. _self.style.top = eleTop + "px";
  46. }
  47. function mouseUp(e) {
  48. var e = e || window.event;
  49. eTime = new Date().getTime();
  50. oPos=[getStyles(_self,'left'),getStyles(_self,'top')];
  51. // console.log("减法结果", eTime - bTime);
  52. // console.log("eTime", eTime);
  53. if (eTime - bTime < 100) {
  54. //如果小于100就是点击事件,而不是拖动事件
  55. // console.log(1)//可以先通过打印判断,条件是否能执行,是否得到预期,
  56. _self.style.left=oPos[0]+'px';
  57. _self.style.top=oPos[1]+'px';
  58. elemClick();
  59. }
  60. removeEvent(document, "mousemove", mouseMove);
  61. removeEvent(document, "mouseup", mouseUp);
  62. }
  63. }
  64. });

封装右键菜单

image.png

  1. Element.prototype.dragNclick = function ( menu,elemClick) {
  2. var bTime = 0,
  3. eTime = 0,
  4. oPos = [],
  5. wWidth = getViewportSize().width,
  6. wHeight = getViewportSize().height,
  7. eleWidth = getStyles(this, "width"),
  8. eleHeight = getStyles(this, "height"),
  9. mWidth=getStyles(menu,'width'),
  10. mHeight=getStyles(menu,'height'),
  11. cbTime=0,
  12. ceTime=0,
  13. counter=0;
  14. drag.call(this); //改变this指向,变成调用的this
  15. function drag() {
  16. var x,
  17. y,
  18. _self = this;
  19. // console.log('this',this)
  20. addEvent(this, "mousedown", function (e) {
  21. // console.log('1');
  22. var e = e || window.event,
  23. btnCode=e.button;//e.button消耗效率,缓存一下比较好
  24. if(btnCode===2){
  25. var mLeft=pagePos(e).X,
  26. mTop=pagePos(e).Y;
  27. if(mLeft<=0){
  28. mLeft=0
  29. }else if(mLeft>=wWidth-mWidth){
  30. mLeft=pagePos(e).X-mWidth;
  31. }
  32. if(mTop<=0){
  33. mTop=0;
  34. }else if(mTop>=wHeight-mHeight){
  35. mTop=pagePos(e).Y-mHeight
  36. }
  37. menu.style.left=mLeft+'px';
  38. menu.style.top=mTop+'px';
  39. menu.style.display='block';
  40. }else if(btnCode===0){
  41. bTime = new Date().getTime();
  42. oPos=[getStyles(_self,'left'),getStyles(_self,'top')];
  43. menu.style.display='none';
  44. // console.log("bTime", bTime);
  45. // console.log(elem);
  46. x = pagePos(e).X - getStyles(_self, "left");
  47. y = pagePos(e).Y - getStyles(_self, "top");
  48. addEvent(document, "mousemove", mouseMove);
  49. addEvent(document, "mouseup", mouseUp);
  50. cancelBubble(e); //保险起见 把阻止事件放进去
  51. preventDefaultEvent(e);
  52. }
  53. });
  54. //阻止默认右键点击事件,右键点击不会触发默认的右键菜单
  55. addEvent(document,'contextmenu',function(e){
  56. var e=e||window.event;
  57. preventDefaultEvent(e);
  58. })
  59. //点击文档区域,让自定义的菜单消失
  60. addEvent(document,'click',function(e){
  61. menu.style.display='none';
  62. })
  63. //阻止冒泡
  64. addEvent(menu,'click',function(e){
  65. var e=e||window.event;
  66. cancelBubble(e);
  67. })
  68. function mouseMove(e) {
  69. // console.log(elem);
  70. var e = e || window.event,
  71. eleLeft = pagePos(e).X - x, //
  72. eleTop = pagePos(e).Y - y;
  73. if (eleLeft <= 0) {
  74. eleLeft = 0;
  75. } else if (eleLeft >= wWidth - eleWidth) {
  76. //
  77. eleLeft = wWidth - eleWidth - 1;
  78. }
  79. if (eleTop <= 0) {
  80. eleTop = 0;
  81. } else if (eleTop >= wHeight - eleHeight) {
  82. eleTop = wHeight - eleHeight - 1;
  83. }
  84. // _self.style.left = pagePos(e).X - x + "px";
  85. // _self.style.top = pagePos(e).Y - y + "px";
  86. _self.style.left = eleLeft + "px";
  87. _self.style.top = eleTop + "px";
  88. }
  89. function mouseUp(e) {
  90. var e = e || window.event;
  91. eTime = new Date().getTime();
  92. oPos = [getStyles(_self, "left"), getStyles(_self, "top")];
  93. // console.log("减法结果", eTime - bTime);
  94. // console.log("eTime", eTime);
  95. if (eTime - bTime < 100) {
  96. //如果小于100就是点击事件,而不是拖动事件
  97. // console.log(1)//可以先通过打印判断,条件是否能执行,是否得到预期,
  98. _self.style.left = oPos[0] + "px";
  99. _self.style.top = oPos[1] + "px";
  100. counter++;
  101. if(counter===1){
  102. cbTime=new Date().getTime();
  103. }
  104. if(counter===2){
  105. ceTime=new Date().getTime();
  106. }
  107. if(cbTime&&ceTime&&(ceTime-cbTime<200)){
  108. elemClick();
  109. // cbTime=0;
  110. // ceTime=0;
  111. // counter=0;
  112. }
  113. t=setTimeout(function(){//如果点一次,隔2秒清零,要不然无限叠加次数
  114. cbTime=0;
  115. ceTime=0;
  116. counter=0;
  117. clearTimeout(t);
  118. },200);
  119. }
  120. removeEvent(document, "mousemove", mouseMove);
  121. removeEvent(document, "mouseup", mouseUp);
  122. }
  123. }
  124. };

多人协作封装

  1. ;(function(){
  2. var Test=function(opt){
  3. }
  4. Test.prototype={
  5. }
  6. window.Test=Test;
  7. })();
  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. <body>
  10. <input type="text" id="num1">
  11. <input type="text" id="num2">
  12. <div class="btn-group">
  13. <button data-sign="plus">+</button>
  14. <button data-sign="minus">-</button>
  15. <button data-sign="mul">*</button>
  16. <button data-sign="div">/</button>
  17. </div>
  18. <script type="text/javascript" src="./utils.js"></script>
  19. <script>
  20. // console.log(document.getElementsByClassName('btn-group')[0])
  21. ; (function () {
  22. var _self;//保存this
  23. var Test = function (opt) {
  24. this.num1 = opt.num1;
  25. this.num2 = opt.num2;
  26. this.btnGroup = opt.btnGroup;
  27. _self = this;
  28. }
  29. Test.prototype = {
  30. init: function () {//
  31. this.bindEvent();
  32. },
  33. bindEvent: function () {
  34. var btns = this.btnGroup;
  35. // _self = this;//不能成功 不能影响compute
  36. // addEvent(btns,'click',function(e){
  37. // });
  38. // addEvent(btns,'click',this.compute.call(this));//执行了 不行
  39. // addEvent(btns,'click',this.compute);
  40. addEvent(btns, 'click', function (e) {
  41. // this.compute.call(_self);//不行
  42. _self.compute.call(_self, e);
  43. })
  44. },
  45. compute: function (e) {
  46. // console.log(e)
  47. // console.log('this',this)//this是btns对象,相当于btns调用
  48. // var val1 = _self.num1.value,
  49. // val2 = _self.num2.value,
  50. // btns = _self.btnGroup;
  51. var val1 = this.num1.value,
  52. tar = e.target || e.srcElement,
  53. val1 =Number(this.num1.value) ,
  54. val2 =Number( this.num2.value),
  55. btns = this.btnGroup,
  56. sign;
  57. sign = tar.getAttribute('data-sign');
  58. switch (sign) {
  59. case 'plus':
  60. console.log(val1+val2)
  61. break;
  62. case 'minus':
  63. console.log(val1-val2)
  64. break;
  65. case 'mul':
  66. console.log(val1*val2)
  67. break;
  68. case 'div':
  69. console.log(val1/val2)
  70. break;
  71. default:
  72. console.log('出错了')
  73. break;
  74. }
  75. }
  76. }
  77. window.Test = Test;
  78. })();
  79. new Test({
  80. num1: document.getElementById('num1'),
  81. num2: document.getElementById('num2'),
  82. btnGroup: document.getElementsByClassName('btn-group')[0]
  83. }).init();
  84. // function addEvent(el, type, fn) {
  85. // // console.log(el);
  86. // if (el.addEventListener) {
  87. // //方法存在
  88. // el.addEventListener(type, fn, false);
  89. // } else if (el.attachEvent) {
  90. // el.attachEvent("on" + type, function () {
  91. // fn.call(el); //将this指向改为调用的本身
  92. // });
  93. // } else {
  94. // el["on" + type] = fn; //相当于el.on,例子el.onclick=func
  95. // }
  96. // }
  97. </script>
  98. </body>
  99. </html>