简单几行代码实现的层拖动、拖放、拖拽效果,几乎兼容所有的浏览器,无调用外部JS插件,完全JavaScript代码实现,兼容IE/Firefox/Chrome等内核的浏览器,分享给前端开发者参考。 示例: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>兼容浏览器最多代码最少的拖动代码</title> </head> <body> <div id="veryhuo" style="width:100px;height:100px;position:absolute;background:red;cursor:move;"></div> <script type="text/javascript"> function drag(o){var a="onmousemove",b="setCapture",c="releaseCapture",f="clientY",g="clientX",d=document,z=d.documentElement,x,y,t,l,w,h;o=d.getElementById(o);o.onmousedown=function(e){e=e||event;x=e[g]-o.offsetLeft;y=e[f]-o.offsetTop;d[a]=function(e){e=e||event;t=e[f]-y;l=e[g]-x;w=z.clientWidth-o.offsetWidth;h=z.clientHeight-o.offsetHeight;l<0&&(l=0);t<0&&(t=0);l>w&&(l=w);t>h&&(t=h);with(o.style){top=t+"px";left=l+"px"}};d.onmouseup=function(){d[a]=null;o[c]&&o[c]()};o[b]&&o[b]();return false}}; drag("veryhuo"); </script> </body> </html><div style="text-align:center;margin:30px 0 0 0;"><hr style="color:#999;height:1px;">如不能显示效果,请按Ctrl+F5刷新本页,更多网页代码:<a href='http://www.veryhuo.com/' target='_blank'>http://www.veryhuo.com/</a></div>提示:可修改后代码再运行! 文章导航 跟随滚动条的网页浮动层,xScrollStick代码CSS实现两个层互相显示,层在层之上的效果