首页 > 语言 > JavaScript > 正文

javascript实现鼠标拖动改变层大小的方法

2024-05-06 16:19:04
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了javascript实现鼠标拖动改变层大小的方法,涉及javascript操作鼠标事件及样式的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了javascript实现鼠标拖动改变层大小的方法。分享给大家供大家参考。具体实现方法如下:

 

 
  1. <html> 
  2. <head> 
  3. <title>拖动改变层的大小</title> 
  4. <meta content="text/html; charset=gb2312" http-equiv="Content-Type"> 
  5. <style> { 
  6. box-sizing: border-box; moz-box-sizing: border-box 
  7. } 
  8. #testDiv { background-color: buttonface; background-repeat: repeat;  
  9. background-attachment: scroll; color: #3969A5; height: 300px;  
  10. left: 30px; overflow: hidden; width: 500; z-index: 2;  
  11. border: 2px outset white; margin: 0px; padding: 2px;  
  12. background-position: 0% 50% } 
  13. body { font-family: Verdana; font-size: 9pt } 
  14. #innerNice { background-color: white; background-repeat: repeat; 
  15. background-attachment:  
  16. scroll; color: #3969A5; height: 100%; overflow: auto;  
  17. width: 100%; border: 2px inset white; padding: 8px;  
  18. background-position: 0% 50% } 
  19. </style> 
  20. <SCRIPT language=javascript> 
  21. var theobject = null;  
  22. function resizeObject() { 
  23. this.el = null; //pointer to the object 
  24. this.dir = ""; //type of current resize (n,s,e,w,ne,nw,se,sw) 
  25. this.grabx = null; //Some useful values 
  26. this.graby = null; 
  27. this.width = null; 
  28. this.height = null; 
  29. this.left = null; 
  30. this.top = null; 
  31. } 
  32. function getDirection(el) { 
  33. var xPos, yPos, offset, dir; 
  34. dir = ""; 
  35. xPos = window.event.offsetX; 
  36. yPos = window.event.offsetY; 
  37. offset = 8; //The distance from the edge in pixels 
  38. if (yPos<offset) dir += "n"; 
  39. else if (yPos > el.offsetHeight-offset) dir += "s"; 
  40. if (xPos<offset) dir += "w"; 
  41. else if (xPos > el.offsetWidth-offset) dir += "e"; 
  42. return dir; 
  43. } 
  44. function doDown() { 
  45. var el = getReal(event.srcElement, "className", "resizeMe"); 
  46. if (el == null) { 
  47. theobject = null; 
  48. return; 
  49. }  
  50. dir = getDirection(el); 
  51. if (dir == "") return; 
  52. theobject = new resizeObject(); 
  53. theobject.el = el; 
  54. theobject.dir = dir; 
  55. theobject.grabx = window.event.clientX; 
  56. theobject.graby = window.event.clientY; 
  57. theobject.width = el.offsetWidth; 
  58. theobject.height = el.offsetHeight; 
  59. theobject.left = el.offsetLeft; 
  60. theobject.top = el.offsetTop; 
  61. window.event.returnValue = false; 
  62. window.event.cancelBubble = true; 
  63. } 
  64. function doUp() { 
  65. if (theobject != null) { 
  66. theobject = null; 
  67. } 
  68. } 
  69. function doMove() { 
  70. var el, xPos, yPos, str, xMin, yMin; 
  71. xMin = 8; //The smallest width possible 
  72. yMin = 8; // height 
  73. el = getReal(event.srcElement, "className", "resizeMe"); 
  74. if (el.className == "resizeMe") { 
  75. str = getDirection(el); 
  76. //Fix the cursor 
  77. if (str == "") str = "default"; 
  78. else str += "-resize"; 
  79. el.style.cursor = str; 
  80. } 
  81. //Dragging starts here 
  82. if(theobject != null) { 
  83. if (dir.indexOf("e") != -1) 
  84. theobject.el.style.width = Math.max(xMin, theobject.width + window.event.clientX - theobject.grabx) + "px"; 
  85. if (dir.indexOf("s") != -1) 
  86. theobject.el.style.height = Math.max(yMin, theobject.height + window.event.clientY - theobject.graby) + "px"; 
  87. if (dir.indexOf("w") != -1) { 
  88. theobject.el.style.left = Math.min(theobject.left + window.event.clientX - theobject.grabx, theobject.left + theobject.width - xMin) + "px"; 
  89. theobject.el.style.width = Math.max(xMin, theobject.width - window.event.clientX + theobject.grabx) + "px"; 
  90. } 
  91. if (dir.indexOf("n") != -1) { 
  92. theobject.el.style.top = Math.min(theobject.top + window.event.clientY - theobject.graby, theobject.top + theobject.height - yMin) + "px"; 
  93. theobject.el.style.height = Math.max(yMin, theobject.height - window.event.clientY + theobject.graby) + "px"; 
  94. } 
  95. window.event.returnValue = false; 
  96. window.event.cancelBubble = true; 
  97. }  
  98. } 
  99. function getReal(el, type, value) { 
  100. temp = el; 
  101. while ((temp != null) && (temp.tagName != "BODY")) { 
  102. if (eval("temp." + type) == value) { 
  103. el = temp; 
  104. return el; 
  105. } 
  106. temp = temp.parentElement; 
  107. } 
  108. return el; 
  109. } 
  110. document.onmousedown = doDown; 
  111. document.onmouseup = doUp; 
  112. document.onmousemove = doMove; 
  113. </SCRIPT> 
  114. </head> 
  115. <body> 
  116. <div class="resizeMe" id="testDiv"> 
  117. <div id="innerNice"> 
  118. <p align="center"> </p> 
  119. <p align="center"> 
  120. 请在边框处拖动鼠标</p> 
  121. <p> </p> 
  122. <p> </p> 
  123. <p> </p> 
  124. </div> 
  125. </div> 
  126. </body> 
  127. </html> 

希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选