zl程序教程

您现在的位置是:首页 >  其他

当前栏目

可以用鼠标拖动的DIV实现思路及代码

思路代码 实现 可以 鼠标 div 拖动
2023-06-13 09:15:06 时间
复制代码代码如下:

<html><head>
<title>测试可动div</title>
<scriptlanguage="javascript"type="text/javascript">
varoffset_x;
varoffset_y;
functionMilan_StartMove(oEvent)
{
varwhichButton;
if(document.all&&oEvent.button==1)whichButton=true;
else{if(oEvent.button==0)whichButton=true;}
if(whichButton)
{
varoDiv=document.getElementById("oDiv");
offset_x=parseInt(oEvent.clientX-oDiv.offsetLeft);
offset_y=parseInt(oEvent.clientY-oDiv.offsetTop);
document.documentElement.onmousemove=function(mEvent)
{
vareEvent;
if(document.all)eEvent=event;
else{eEvent=mEvent;}
varoDiv=document.getElementById("oDiv");
varx=eEvent.clientX-offset_x;
vary=eEvent.clientY-offset_y;
oDiv.style.left=(x)+"px";
oDiv.style.top=(y)+"px";
}
}
}
functionMilan_StopMove(oEvent){document.documentElement.onmousemove=null;}
</script>
</head>
<body>
<divid="oDiv"onmousedown="Milan_StartMove(event)"onmouseup="Milan_StopMove(event)"

style="cursor:move;position:absolute;width:100px;height:60px;border:1pxsolid

silver;left:100px;top:100px;z-index:9999;"></div>
</body></html>

document.all[]是文档中所有标签组成的一个数组变量,包括了文档对象中所有元素;
event.button的值:0没按键1按左键2按右键3按左和右键4按中间键5按左和中间键6按右和中间键7按所有的键

下面对此代码进行改进,模仿window,并且让它可以盖住select
复制代码代码如下:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>测试可动div</title>
<scriptlanguage="javascript"type="text/javascript">
varoffset_x;
varoffset_y;
functionMilan_StartMove(oEvent,div_id)
{
varwhichButton;
if(document.all&&oEvent.button==1)whichButton=true;
else{if(oEvent.button==0)whichButton=true;}
if(whichButton)
{
varoDiv=div_id;
offset_x=parseInt(oEvent.clientX-oDiv.offsetLeft);
offset_y=parseInt(oEvent.clientY-oDiv.offsetTop);
document.documentElement.onmousemove=function(mEvent)
{
vareEvent;
if(document.all)eEvent=event;
else{eEvent=mEvent;}
varoDiv=div_id;
varx=eEvent.clientX-offset_x;
vary=eEvent.clientY-offset_y;
oDiv.style.left=(x)+"px";
oDiv.style.top=(y)+"px";
vard_oDiv=document.getElementById("disable_"+oDiv.id);
d_oDiv.style.left=(x)+"px";
d_oDiv.style.top=(y)+"px";
}
}
}
functionMilan_StopMove(oEvent){document.documentElement.onmousemove=null;}
functiondiv_Close(o)
{varoDiv=o;oDiv.style.display="none";vard_oDiv=document.getElementById("disable_"+o.id);d_oDiv.style.display="none";}
</script>
</head>
<body>
<divid="oDiv"style="position:absolute;width:100px;height:60px;border:1pxsolidsilver;left:100px;top:100px;z-index:9999;">
<divid="move"onmousedown="Milan_StartMove(event,this.parentNode)"onmouseup="Milan_StopMove(event)"
style="cursor:move;width:100%;height:15px;background-color:#0066cc;font-size:10px;">
<divonclick="div_Close(this.parentNode.parentNode)"style="float:right;width:10px;height:100%;cursor:hand;background-color:#cc3333;color:White;font-size:15px;">X</div>
</div>
<span>测试一下</span>
</div>
<divid="disable_oDiv"style="position:absolute;left:100px;top:100px;width:100px;height:60px;z-index:9998;FILTER:alpha(opacity=50);";>
<iframesrc="about:blank"name="hiddenIframe"width="100%"frameborder="0"height="60px"title="遮盖层"></iframe></div>
<selectname="ListHead1$DropDownList3"id="ListHead1_DropDownList3">
<optionselected="selected"value=""></option>
<optionvalue="2">3333</option>
<optionvalue="6">1111</option>
<optionvalue="B">222</option>
</select>
</body>
</html>

现在这个可拖动的div是不是好很多了?不用担心select了。之前放出来的只能在IE下正常工作,主要是用了parentElement,现在我把它换成parentNode,调整了CSS样式,这样在FF下也能正常运行了。