javascript 调整宽度和高度的div

 就是用js控制一下div的宽和高,可以用鼠标拖拽的,不多说了,代码如下:
  1. function getPosition(obj)
  2.     var top = 0,left = 0;
  3.     do 
  4.     {
  5.        top += obj.offsetTop;
  6.        left += obj.offsetLeft;
  7.     }
  8.     while ( obj = obj.offsetParent );      
  9.     var arr = new Array();      
  10.     arr[0] = top;
  11.     arr[1] = left;    
  12.     return arr;    
  13. }
  14. var moveFlag=false;
  15. var moveLeft=false;
  16. var moveRight=false;
  17. var moveTop=false;
  18. var moveBom=false;
  19. function ShowMouseStyle()
  20. {
  21.     var x=event.clientX;
  22.     var y=event.clientY;
  23.     var div=document.getElementById("main");
  24.     var divLeft= getPosition(div)[0];
  25.     var divTop=getPosition(div)[1];
  26.     var divRight=divLeft+parseInt(div.style.width);
  27.     var divBom=divTop+parseInt(div.style.height);
  28.     if(Math.abs(x-divLeft)<3||moveLeft) 
  29.     {
  30.           div.style.cursor="w-resize";
  31.           if(moveFlag) 
  32.           MoveLeft(divLeft,div);
  33.     }
  34.     else if(Math.abs(x-divRight)<3||moveRight) 
  35.     {   
  36.           div.style.cursor="w-resize";
  37.           if(moveFlag) 
  38.           MoveRight(divRight,div); 
  39.     }   
  40.     else if(Math.abs(y-10-divTop)<3||moveTop) 
  41.     {
  42.           div.style.cursor="n-resize";
  43.           if(moveFlag) 
  44.           MoveTop(divTop,div)
  45.     }
  46.     else if(Math.abs(y-5-divBom)<3||moveBom) 
  47.     {
  48.           div.style.cursor="n-resize";
  49.           if(moveFlag) 
  50.           MoveBom(divBom,div) 
  51.     } 
  52.     else
  53.     {
  54.          div.style.cursor="";
  55.     } 
  56. }
  57. function move(flag)
  58. {
  59.   moveFlag=flag;
  60.   if(!flag)
  61.   {
  62.      moveLeft=false;
  63.      moveRight=false;
  64.      moveTop=false;
  65.      moveBom=false;
  66.   }
  67. }
  68. function MoveLeft(leftPx,o)
  69. {
  70.    var r=leftPx-event.clientX;
  71.    if((parseInt(o.style.width)+r)>100&&(parseInt(o.style.width)+r)<600)
  72.    o.style.width=parseInt(o.style.width)+r;
  73.    moveLeft=true;
  74. }
  75. function MoveRight(rightPx,o)
  76. {
  77.    var r=event.clientX-rightPx;
  78.    if((parseInt(o.style.width)+r)>100&&(parseInt(o.style.width)+r)<600)
  79.    o.style.width=parseInt(o.style.width)+r;
  80.    moveRight=true;
  81. }
  82. function MoveTop(topPx,o)
  83. {
  84.    var r=event.clientY-topPx;
  85.    if((parseInt(o.style.height)+r)>100&&(parseInt(o.style.height)+r)<500)
  86.    o.style.height=parseInt(o.style.height)+r;
  87.    moveTop=true;
  88. }
  89. function MoveBom(bomPx,o)
  90. {
  91.    var r=event.clientY-bomPx;
  92.    if((parseInt(o.style.height)+r)>100&&(parseInt(o.style.height)+r)<500)
  93.    o.style.height=parseInt(o.style.height)+r;
  94.    moveBom=true;
  95. }
html代码:
  1. <body onmousemove="ShowMouseStyle()" onmousedown="move(true)"  onmouseup="move(false)">
  2. <div id="main" style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid; border-bottom: 1px solid; width:101px; height:101px;">
  3. </div>
  4. </body>
--------------------------结束------------------------
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值