您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

js实现div在页面拖动效果

2026/2/4 21:29:43发布8次查看
本文实例讲述了js实现div在页面拖动效果。分享给大家供大家参考,具体如下:
<style type="text/css"> body { margin: 0px; } #div1 { display: none; position: absolute; z-index: 1000; height: 100%; width: 100%; background: #000000; filter:alpha(opacity=30); } #div2 { display: none; position: absolute; height: 100%; width: 100%; padding-top: 10%; z-index: 1001; } #div3 { display: block; position: absolute; z-index: 999; } </style> <script type="text/javascript"> //定义移动对象和移动坐标 var mouse_obj="none",_x,_y; //拖动对象函数(自动) document.onmousemove=function() { if(mouse_obj!=="none") { document.getelementbyid(mouse_obj).style.left=_x+event.x; document.getelementbyid(mouse_obj).style.top=_y+event.y; event.returnvalue=false; } } //停止拖动函数(自动) document.onmouseup=function() { mouse_obj="none"; } //确定被拖动对象函数 o为被拖动对象 function m(o) { mouse_obj=o; _x=parseint(document.getelementbyid(mouse_obj).style.left)-event.x; _y=parseint(document.getelementbyid(mouse_obj).style.top)-event.y; } </script> <div id="div1"></div> <div id="div2" onmousedown="m(this.id)" style="left: 0px;top: 0px;"> <table width="50%" border="0" cellpadding="3" cellspacing="1" style="background: #ff7300; position:static;filter:progid:dximagetransform.microsoft.dropshadow (color=#666666,offx=4,offy=4,positives=true)" align="left"> <tr style="cursor: move;"> <td><font color="#ffffff">温馨提示:</font></td> <td align="right"><input type="button" value="x" onclick="document.getelementbyid ('div1').style.display='none';document.getelementbyid ('div2').style.display='none';" style="cursor: hand;"></td> </tr> <tr> <td colspan="2" width="100%" bgcolor="#ffffff" height="150" align="middle">欢迎访问 <a href="http://www.jb51.net">http://www.jb51.net</a></td> </tr> </table> </div> <div id="div3"><input type="button" value="打开层" onclick="document.getelementbyid ('div1').style.display='block';document.getelementbyid ('div2').style.display='block';"></div>
希望本文所述对大家javascript程序设计有所帮助。
更多js实现div在页面拖动效果。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product