一个简单的拖动(JS原生)

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <style type="text/css">
 5 #main div{position:absolute;width:220px;height:150px;border:1px solid #999;}
 6 </style>
 7 <script type="text/javascript">
 8 var a;
 9 document.onmouseup=function(){if(!a)return;document.all?a.releaseCapture():window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);a="";};
10 document.onmousemove=function (d){if(!a)return;if(!d)d=event;a.style.left=(d.clientX-b)+"px";a.style.top=(d.clientY-c)+"px";};
11 function $(o,e){a=o;document.all?a.setCapture():window.captureEvents(Event.MOUSEMOVE);b=e.clientX-parseInt(a.style.left);c=e.clientY-parseInt(a.style.top);}
12 </script>
13 </head>
14 <body>
15   <div id="main">
16     <div style="left:100px;top:100px;background:#fc9;" onmousedown="$(this,event)">1</div>
17     <div style="left:400px;top:100px;background:#9cf;" onmousedown="$(this,event)">2</div>
18     <div style="left:700px;top:100px;background:#f9c;" onmousedown="$(this,event)">3</div>
19     <div style="left:100px;top:300px;background:#9fc;" onmousedown="$(this,event)">4</div>
20     <div style="left:400px;top:300px;background:#c9f;" onmousedown="$(this,event)">5</div>
21     <div style="left:700px;top:300px;background:#cf9;" onmousedown="$(this,event)">6</div>
22   </div>
23 </body>
24 </html>

 

posted @ 2017-02-27 20:39  欲戴王冠,必承其重  阅读(139)  评论(0编辑  收藏  举报