改造移动DIV

<script type="text/javascript">
 //保留的位置
 var saveLeft,saveTop,saveWidth,saveHeight;
 var theBody;
 var eventType;    //事件种类, "move"、"resize"
 var div;
 
 //创建并设定div的参数
 function setDiv()
 {
  //防止重复打开
  if (div)
  {
   return;
  }
  var newLeft,newTop,newWidth,newHeight;
  theBody = document.body;
 
  div = document.createElement("div");
  div.id = "panelDiv";
  div.style.position = "absolute";
  div.style.backgroundColor = "#00CBFE"
  div.style.padding = "2px 3px 3px 2px";
  div.style.overflow = "hidden";
  div.style.zIndex = 1;
   div.style.width =  "300px";;
   div.style.height =  "200px";;
   div.style.left = 200;
   div.style.top = 20;
   div = setChild(div);
  theBody.appendChild(div);
 }
 
 function setChild(div)
 {
  //可否移动、调整
  var isMove =true;
  var isResize =false;
 
  //底色
  var cDiv = document.createElement;
  var backDiv = cDiv("div");
  backDiv.style.cssText = "left: 0px; top: 0px; width: 100%; height: 100%; background-color: #CCF0FF;";
  backDiv.setAttribute("onmousedown", function(){setMove(this)});
  div.appendChild(backDiv);
 
  //标题
  var topDiv = cDiv("div");
  topDiv.style.cssText = "left: 2px; top: 2px; width: 100%; height: 30px; position: absolute; background-color: #78ABFF; vertical-align: middle; z-index: 5";

   topDiv.style.cursor = "move";
   topDiv.setAttribute("onmousedown", function(){setMove(this)});

  topDiv.innerHTML = "<span style='top: 5px; left:5px; font-size: 20px; font-weight: bold; color: #102548; position: relative;' onselectstart='return false'>标题栏</span>";
  div.appendChild(topDiv);
 
  //关闭按钮
  var closeDiv = cDiv("div");
  closeDiv.style.cssText = "right: 5px; top : 2px; width: 24px; height: 15px; position: absolute; background-color: #E4EEFF; border: #2D66C4 1px solid; text-align: center; vertical-align: middle; cursor: pointer; z-index: 10";
  closeDiv.setAttribute("onclick", function() {eCloseDiv()});
  closeDiv.innerHTML = "<span style='font-size: 18px; font-weight: bold; color: #0E377A;' title='Esc快捷键'>×</span>";
  div.appendChild(closeDiv);
 
  //内容
  var contentDiv = cDiv("div");
  contentDiv.style.cssText = "left: 2px; top: 35px; width: 100%; position: absolute; overflow: auto";
  contentDiv.style.height = (parseInt(div.style.height) - 40) + "px";
  contentDiv.innerHTML = "<table style='width: 100%;  text-align: center; vertical-align: hidden'><tr><td vAlign=top >";
   contentDiv.innerHTML +="<input type='text' size=20/>  ";
   contentDiv.innerHTML +="</td></tr></table>";
  contentDiv.setAttribute("onmousedown", function(){setMove(this)});
  contentDiv.style.cursor="hand";
  div.appendChild(contentDiv);
 
  //调整大小
  var reDiv = cDiv("div");
  reDiv.style.cssText = "right: 0px; bottom: 0px; width: 5px; height: 5px; position: absolute;";
  if (isResize)
  {
   reDiv.style.cursor = "se-resize";
   reDiv.setAttribute("onmousedown", function(){setResize(this)});
  }
  else
  {
   reDiv.style.cursor = "default";
  }
  div.appendChild(reDiv);
  return div;
 }
 
 var oX, oY, oLeft, oTop, oWidth, oHeight; //存储原始移动前的位置
 var divClone, oDiv;   //克隆的节点和原始节点
 var oTime;
 //clone拖移的节点
 function setMove(obj)
 {
  if (event.button == 1)
  {
   if (oTime)
   {
    clearTimeout(oTime);
    divClone.parentNode.removeChild(divClone);
   }
   oDiv = obj.parentNode;
   divClone = oDiv.cloneNode(true);
   divClone.style.filter = "Alpha(opacity=40)";
   divClone.childNodes[1].setAttribute("onmousemove", function(){startMove(this)});
   divClone.childNodes[1].setAttribute("onmouseup", function(){endMove()});
   oX = parseInt(event.clientX);
   oY = parseInt(event.clientY);
   oLeft = parseInt(divClone.style.left);
   oTop = parseInt(divClone.style.top);
   document.body.appendChild(divClone);
   divClone.childNodes[1].setCapture();
   eventType = "move";
  }
 }
 
 //拖移
 function startMove(obj)
 {
  if (eventType == "move" && event.button == 1)
  {
   var moveDiv = obj.parentNode;
   moveDiv.style.left = (oLeft + event.clientX - oX) + "px";
   moveDiv.style.top = (oTop + event.clientY - oY) + "px";
  }
 }
 
 //拖移结束调用动画
 function endMove()
 {
  if (eventType == "move")
  {
   divClone.childNodes[1].releaseCapture();
            move(parseInt(divClone.style.left), parseInt(divClone.style.top));
   eventType = "";
  }
 }
 
 //移动的动画
 function move(aimLeft, aimTop)
 {
  var nowLeft = parseInt(oDiv.style.left);
  var nowTop = parseInt(oDiv.style.top);
  var moveSize = 30;
  if (nowLeft > aimLeft + moveSize || nowLeft < aimLeft - moveSize || nowTop > aimTop + moveSize || nowTop < aimTop - moveSize)
  {
   oDiv.style.left = aimLeft > nowLeft + moveSize ? (nowLeft + moveSize) + "px" : aimLeft < nowLeft - moveSize ? (nowLeft - moveSize) + "px" : nowLeft + "px";
   oDiv.style.top = aimTop > nowTop + moveSize ? (nowTop + moveSize) + "px" : aimTop < nowTop - moveSize ? (nowTop - moveSize) + "px" : nowTop + "px";
   oTime = setTimeout("move(" + aimLeft + ", " + aimTop + ")", 1);
  }
  else
  {
   oDiv.style.left = divClone.style.left;
   oDiv.style.top = divClone.style.top;
   divClone.parentNode.removeChild(divClone);
   divClone == null;
  }
 }
 
 //clone调整大小的节点
 function setResize(obj)
 {
  if (event.button == 1)
  {
   if (oTime)
   {
    clearTimeout(oTime);
    divClone.parentNode.removeChild(divClone);
   }
   oDiv = obj.parentNode;
   divClone = oDiv.cloneNode(true);
   divClone.style.filter = "Alpha(opacity=50)";
   divClone.childNodes[4].setAttribute("onmousemove", function(){startResize(this)});
   divClone.childNodes[4].setAttribute("onmouseup", function(){endResize()});
   oX = parseInt(event.clientX);
   oY = parseInt(event.clientY);
   oWidth = parseInt(divClone.style.width);
   oHeight = parseInt(divClone.style.height);
   document.body.appendChild(divClone);
   divClone.childNodes[4].setCapture();
   eventType = "resize";
  }
 }
 
 //拖动调整大小
 function startResize(obj)
 {
  if (eventType == "resize" && event.button == 1)
  {
   var nX = event.clientX;
   var nY = event.clientY;
   if (nX > oX - oWidth && nY > oY - oHeight + 40)
   {
    var resizeDiv = obj.parentNode;
    resizeDiv.style.width = (oWidth + event.clientX - oX) + "px";
    resizeDiv.style.height = (oHeight + event.clientY - oY) + "px";
    resizeDiv.childNodes[3].style.height = (parseInt(resizeDiv.style.height) - 40) + "px";
   }
  }
 }
 
 //调整大小结束
 function endResize()
 {
  if (eventType == "resize")
  {
   divClone.childNodes[4].releaseCapture();
            resize(parseInt(divClone.style.width), parseInt(divClone.style.height));
   eventType = "";
  }
 }
 
 //调整大小的动画
 function resize(aimWidth, aimHeight)
 {
  var nowWidth = parseInt(oDiv.style.width);
  var nowHeight = parseInt(oDiv.style.height);
  var resizeSize = 30;
  if (nowWidth > aimWidth + resizeSize || nowWidth < aimWidth - resizeSize || nowHeight > aimHeight + resizeSize || nowHeight < aimHeight - resizeSize)
  {
   oDiv.style.width = aimWidth > nowWidth + resizeSize ? (nowWidth + resizeSize) + "px" : aimWidth < nowWidth - resizeSize ? (nowWidth - resizeSize) + "px" : nowWidth + "px";
   oDiv.style.height = aimHeight > nowHeight + resizeSize ? (nowHeight + resizeSize) + "px" : aimHeight < nowHeight - resizeSize ? (nowHeight - resizeSize) + "px" : nowHeight + "px";
   oDiv.childNodes[3].style.height = (parseInt(oDiv.style.height) - 40) + "px";
   oTime = setTimeout("resize(" + aimWidth + ", " + aimHeight + ")", 1);
  }
  else
  {
   oDiv.style.width = divClone.style.width;
   oDiv.style.height = divClone.style.height;
   oDiv.childNodes[3].style.height = (parseInt(oDiv.style.height) - 40) + "px";
   divClone.parentNode.removeChild(divClone);
   divClone == null;
  }
 }
 
 //关闭DIV
 function eCloseDiv()
 {
  if (div)
  {
   div.parentNode.removeChild(div);
   var ipt = document.getElementsByTagName("input");
   for(var i = 0; i < ipt.length; i++)
   {
    ipt[i].disabled = false;
   }
   div = null;
  }
 }
 

 
 //快捷键
 document.onkeydown = function()
 {
  event.keyCode == 27 ? eCloseDiv() : null;  //Esc快捷键
  !event.ctrlKey && (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) ? arrowMove(event.keyCode) : null;
  event.ctrlKey && (event.keyCode == 37  || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) ? arrowResize(event.keyCode) : null;
 }
 
 //上下左右箭头移动div
 function arrowMove(eKeyCode)
 {
  if (div)
  {
   if (isMove)
   {
    switch(eKeyCode)
    {
     case 37:
      div.style.left = (parseInt(div.style.left) - 1) + "px"; //left
      break
     case 38:
      div.style.top = (parseInt(div.style.top) - 1) + "px"; //up
      break
     case 39:
      div.style.left = (parseInt(div.style.left) + 1) + "px"; //right
      break
     case 40:
      div.style.top = (parseInt(div.style.top) + 1) + "px"; //down
      break
    }
   }
  }
 }
 
 //ctrl+上下左右箭头调整div大小
 function arrowResize(eKeyCode)
 {
  if (div)
  {
   if (isResize)
   {
    switch(eKeyCode)
    {
     case 37:
      div.style.width = (parseInt(div.style.width) - 1) + "px"; //left
      break
     case 38:
      div.style.height = (parseInt(div.style.height) - 1) + "px"; //up
      break
     case 39:
      div.style.width = (parseInt(div.style.width) + 1) + "px"; //right
      break
     case 40:
      div.style.height = (parseInt(div.style.height) + 1) + "px"; //down
      break
    }
   }
  }
 }
</script>
<body bgcolor="lightgreen">
<script language="JavaScript" >
//调用入口,实际页面中把body去掉即可
setDiv();
</script>
</body>

 

posted @ 2008-02-20 17:37  Microbar  阅读(650)  评论(0编辑  收藏  举报