div实现拖拽效果,宽度发生变化

比眉伴天荒 2022-08-20 09:27 279阅读 0赞

最近项目中,需要写个能够拖拽的div,从网上找了相关的代码,为了方便以后使用,记下。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html style="height:100%;">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>div width resize</title>
  6. <!--引用jquery-->
  7. <script src="http://code.jquery.com/jquery-1.8.0.min.js" type="text/javascript"></script>
  8. <script type="text/javascript">
  9. function bindResize(el)
  10. {
  11. //初始化参数
  12. var els = document.getElementById('menu').style;
  13. //鼠标的 X 和 Y 轴坐标
  14. x = 0;
  15. //邪恶的食指
  16. $(el).mousedown(function (e)
  17. {
  18. //按下元素后,计算当前鼠标与对象计算后的坐标
  19. x = e.clientX - el.offsetWidth - $("#menu").width();
  20. //在支持 setCapture 做些东东
  21. el.setCapture ? (
  22. //捕捉焦点
  23. el.setCapture(),
  24. //设置事件
  25. el.onmousemove = function (ev)
  26. {
  27. mouseMove(ev || event);
  28. },
  29. el.onmouseup = mouseUp
  30. ) : (
  31. //绑定事件
  32. $(document).bind("mousemove", mouseMove).bind("mouseup", mouseUp)
  33. );
  34. //防止默认事件发生
  35. e.preventDefault();
  36. });
  37. //移动事件
  38. function mouseMove(e)
  39. {
  40. //宇宙超级无敌运算中...
  41. els.width = e.clientX - x + 'px';
  42. }
  43. //停止事件
  44. function mouseUp()
  45. {
  46. //在支持 releaseCapture 做些东东
  47. el.releaseCapture ? (
  48. //释放焦点
  49. el.releaseCapture(),
  50. //移除事件
  51. el.onmousemove = el.onmouseup = null
  52. ) : (
  53. //卸载事件
  54. $(document).unbind("mousemove", mouseMove).unbind("mouseup", mouseUp)
  55. );
  56. }
  57. }
  58. var divResize=function(){
  59. var totalHeight=$("html").height();
  60. console.log(totalHeight);
  61. var topHeight=$("#top").height()
  62. $("#menu").height(totalHeight-topHeight);
  63. $("#rightbar").height(totalHeight-topHeight);
  64. }
  65. $(function() {
  66. divResize();
  67. $(window).resize(divResize);
  68. bindResize(document.getElementById('rightbar'));
  69. });
  70. </script>
  71. <style type="text/css">
  72. .content {
  73. width: 200px;
  74. background: #f1f1f1;
  75. text-align: center;
  76. border-color: #CCCCCC;
  77. border-style: solid;
  78. border-width: 0 1px;
  79. }
  80. </style>
  81. </head>
  82. <body style="padding: 0; margin: 0;">
  83. <!--
  84. <table style="height: 100%">
  85. <tr>
  86. <td id="menu" class="content"></td>
  87. <td id="rightbar"
  88. style="width: 2px; background: #cccccc; cursor: e-resize;"></td>
  89. </tr>
  90. </table>
  91. -->
  92. <div>
  93. <div id="top" style="width: 100%; height: 80px;"></div>
  94. <div style="float: left;" id="menu" class="content">
  95. <span>待拖拽的div</span>
  96. </div>
  97. <div id="rightbar"
  98. style="width: 2px; background: #cccccc; cursor: e-resize; float: left;"></div>
  99. </div>
  100. <div id="right">
  101. 右边的div
  102. </div>
  103. </body>
  104. </html>

发表评论

表情:
评论列表 (有 0 条评论,279人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JavaScript 效果实现

      我们在访问一些网站时,会发现有些内容是可以拖拽的,比较方便,那么使用JS怎么实现呢?今天结合代码展示下拖拽效果的实现。  这里需要先熟悉下几个JS事件: onmouse