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

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

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

图片精选