//关闭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; } }
//关闭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; } }
obj.style.top = "50px";
这样就可以了 。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<!-- DW6 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>shawl.qiu template</title>
<script type="text/javascript">
function fDragging(obj, e, limit){
obj.style.cursor="move"
e=e||window.event;
var x=parseInt(obj.offsetLeft);
var y=parseInt(obj.offsetTop);
var x_=e.clientX-x;
var y_=e.clientY-y;
if(document.addEventListener){
document.addEventListener('mousemove', inFmove, true);
document.addEventListener('mouseup', inFup, true);
}
else if(document.attachEvent){
document.attachEvent('onmousemove', inFmove);
document.attachEvent('onmouseup', inFup);
}
inFstop(e); inFabort(e)
function inFmove(e){
var evt;
if(!e)e=window.event; if(limit){
var op=obj.parentNode;
var opX=parseInt(op.style.left);
var opY=parseInt(op.style.top);
if((e.clientX-x_)<0) return false;
else if((e.clientX-x_+obj.offsetWidth+opX)>(opX+op.offsetWidth)) return false;
if(e.clientY-y_<0) return false;
else if((e.clientY-y_+obj.offsetHeight+opY)>(opY+op.offsetHeight)) return false;
//status=e.clientY-y_;
} obj.style.left=e.clientX-x_+'px';
obj.style.top=e.clientY-y_+'px'; inFstop(e);
} // shawl.qiu script function inFup(e){
var evt;
if(!e)e=window.event; if(document.removeEventListener){
document.removeEventListener('mousemove', inFmove, true);
document.removeEventListener('mouseup', inFup, true);
}
else if(document.detachEvent){
document.detachEvent('onmousemove', inFmove);
document.detachEvent('onmouseup', inFup);
}
inFstop(e);
} // shawl.qiu script
function inFstop(e){
if(e.stopPropagation) return e.stopPropagation();
else return e.cancelBubble=true;
} // shawl.qiu script
function inFabort(e){
if(e.preventDefault) return e.preventDefault();
else return e.returnValue=false;
} // shawl.qiu script
}</script>
</head>
<body>
<div style=" border:1px dashed blue; width: 760px; height:600px; text-align:center; position:absolute; left:100px; top: 10px;"> this parent
<div style=" border:1px dashed blue; width: 180px; text-align:center; position:absolute; left:50px; top: 50px;" onmousedown="fDragging(this, event, true);">
element <br/>
dragging compatibility for IE, Opera, Firefox.
</div>
<div style=" border:1px dashed blue; width: 180px; text-align:center; position:absolute; left:100px; top: 150px;" onmousedown="fDragging(this, event, true);">
element 1<br/>
dragging compatibility for IE, Opera, Firefox.
</div>
<div style=" border:1px dashed blue; width: 180px; text-align:center; position:absolute; left:200px; top: 250px;" onmousedown="fDragging(this, event, false);">
element 2<br/>
dragging compatibility for IE, Opera, Firefox. <br/>
<font color="red">dragging everywhere</font>
</div>
</div>
</body>
</html><HTML></HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Div拖动</title>
</head>
<mce: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 = "#E5E5E5"
div.style.padding = "2px 5px 5px 2px";
div.style.overflow = "hidden";
div.style.zIndex = 1;
//设定打开的大小和位置
Function()
{
var openType = document.getElementById("radio1").checked ? 0 : 1;
if (openType == 0) //默认大小默认位置居中打开
{
newWidth = "300px";
newHeight = "300px";
newLeft = (theBody.clientWidth - parseInt(newWidth)) / 2 + "px";
newTop = (theBody.clientHeight - parseInt(newHeight)) / 2 + "px";
}
else //存储的位置与大小
{
newWidth = saveWidth ? saveWidth : "300px";
newHeight = saveHeight ? saveHeight : "300px";
newLeft = saveLeft ? saveLeft : (theBody.clientWidth - parseInt(newWidth)) / 2 + "px";
newTop = saveTop ? saveTop : (theBody.clientHeight - parseInt(newHeight)) / 2 + "px";
}
div.style.width = newWidth;
div.style.height = newHeight;
div.style.left = newLeft;
div.style.top = newTop;
}
div = setChild(div);
theBody.appendChild(div);
var ipt = document.getElementsByTagName("input");
for(var i = 0; i < ipt.length; i++)
{
ipt[i].disabled = true;
}
}
function setChild(div)
{
//可否移动、调整
var isMove = document.getElementById("isMove").checked;
var isResize = document.getElementById("isResize").checked;
//底色
var cDiv = document.createElement;
var backDiv = cDiv("div");
backDiv.style.cssText = "left: 0px; top: 0px; width: 100%; height: 100%; background-color: #F5F5F5;";
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";
if (isMove)
{
topDiv.style.cursor = "move";
topDiv.setAttribute("onmousedown", function(){setMove(this)});
}
else
{
topDiv.style.cursor = "default";
}
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: 8px; top : 5px; width: 24px; height: 24px; 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: 20px; font-weight: bold; color: #0E377A;" mce_style="font-size: 20px; 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%; height: 100%; text-align: center; vertical-align: hidden'><tr><td><p>这里是内容区!</p><a href="javascript:saveDiv()" mce_href="javascript:saveDiv()">保留这个位置和大小</a></td></tr></table>";
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=50)";
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;
}
}
//保留位置和大小
function saveDiv()
{
if (div)
{
saveLeft = div.style.left;
saveTop = div.style.top;
saveWidth = div.style.width;
saveHeight = div.style.height;
alert("保留成功!");
}
}
//快捷键
document.onkeydown = function()
{
event.keyCode == 27 ? eCloseDiv() : null; //Esc快捷键
event.ctrlKey && (event.keyCode == 83 ||
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Div拖动</title>
</head>
<mce: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 = "#E5E5E5"
div.style.padding = "2px 5px 5px 2px";
div.style.overflow = "hidden";
div.style.zIndex = 1;
//设定打开的大小和位置
Function()
{
var openType = document.getElementById("radio1").checked ? 0 : 1;
if (openType == 0) //默认大小默认位置居中打开
{
newWidth = "300px";
newHeight = "300px";
newLeft = (theBody.clientWidth - parseInt(newWidth)) / 2 + "px";
newTop = (theBody.clientHeight - parseInt(newHeight)) / 2 + "px";
}
else //存储的位置与大小
{
newWidth = saveWidth ? saveWidth : "300px";
newHeight = saveHeight ? saveHeight : "300px";
newLeft = saveLeft ? saveLeft : (theBody.clientWidth - parseInt(newWidth)) / 2 + "px";
newTop = saveTop ? saveTop : (theBody.clientHeight - parseInt(newHeight)) / 2 + "px";
}
div.style.width = newWidth;
div.style.height = newHeight;
div.style.left = newLeft;
div.style.top = newTop;
}
div = setChild(div);
theBody.appendChild(div);
var ipt = document.getElementsByTagName("input");
for(var i = 0; i < ipt.length; i++)
{
ipt[i].disabled = true;
}
}
function setChild(div)
{
//可否移动、调整
var isMove = document.getElementById("isMove").checked;
var isResize = document.getElementById("isResize").checked;
//底色
var cDiv = document.createElement;
var backDiv = cDiv("div");
backDiv.style.cssText = "left: 0px; top: 0px; width: 100%; height: 100%; background-color: #F5F5F5;";
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";
if (isMove)
{
topDiv.style.cursor = "move";
topDiv.setAttribute("onmousedown", function(){setMove(this)});
}
else
{
topDiv.style.cursor = "default";
}
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: 8px; top : 5px; width: 24px; height: 24px; 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: 20px; font-weight: bold; color: #0E377A;" mce_style="font-size: 20px; 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%; height: 100%; text-align: center; vertical-align: hidden'><tr><td><p>这里是内容区!</p><a href="javascript:saveDiv()" mce_href="javascript:saveDiv()">保留这个位置和大小</a></td></tr></table>";
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=50)";
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;
}
}
//保留位置和大小
function saveDiv()
{
if (div)
{
saveLeft = div.style.left;
saveTop = div.style.top;
saveWidth = div.style.width;
saveHeight = div.style.height;
alert("保留成功!");
}
}
//快捷键
document.onkeydown = function()
{
event.keyCode == 27 ? eCloseDiv() : null; //Esc快捷键
event.ctrlKey && (event.keyCode == 83 ||
+1obj.style.top = obj.offsetTop++ +"px";
obj.style.left= obj.offsetLeft++ +"px";;
绝对定位、改变left、top即可!!一楼正解!