用js绘制矩形框

来源:互联网 发布:网络歌曲大全2017 编辑:程序博客网 时间:2024/05/29 18:07
 

今天读John Resig的Pro Javascript Techniques时候看到他书上给的一个关于drag and drop的例子,我做的第二件事就是js模拟用鼠标拖出矩形框,代码很简单,如果不考虑把整个代码封装起来, 大约5分钟里也可以写出类似的效果,为了把整个代码封装成一个对象,稍微借鉴了Pro Javascript Techniques书中的代例子的风格。

Pro Javascript Techniques书中推荐的drag and drop 例子:http://boring.youngpup.net/2001/domdrag 

运行代码

view plaincopy to clipboardprint?
  1. <html>      
  2.     <head>      
  3.     <title>Rect</title>      
  4.     <style type="text/css">   
  5.     </style>      
  6.     <script type="text/javascript">  
  7.         var Rect = {   
  8.                         //当前正在画的矩形对象   
  9.                 obj: null,  
  10.                 //画布   
  11.                 container: null,  
  12.                 //初始化函数   
  13.                 init: function(containerId){  
  14.                     Rect.container = document.getElementById(containerId);   
  15.                     if(Rect.container){  
  16.                                 //鼠标按下时开始画   
  17.                         Rect.container.onmousedown = Rect.start;  
  18.                     }  
  19.                     else{  
  20.                         alert('You should specify a valid container!');  
  21.                     }  
  22.                 },  
  23.                 start: function(e){  
  24.                     var o = Rect.obj = document.createElement('div');  
  25.                     o.style.position = "absolute";  
  26.                     // mouseBeginX,mouseBeginY是辅助变量,记录下鼠标按下时的位置   
  27.   
  28.                     o.style.left = o.mouseBeginX = Rect.getEvent(e).x;  
  29.                     o.style.top = o.mouseBeginY = Rect.getEvent(e).y;  
  30.                     o.style.height = 0;  
  31.                     o.style.width = 0;  
  32.                     o.style.border = "dotted black 1px";  
  33.                     //向o添加一个叉叉,点击叉叉可以删除这个矩形   
  34.                     var deleteLink = document.createElement('a');  
  35.                     deleteLink.href="#";  
  36.                     deleteLink.onclick = function(){  
  37.                             Rect.container.removeChild(this.parentNode);  
  38.                             //this.parentNode.style.display = "none";   
  39.                             //alert(this.tagName);   
  40.                     }  
  41.                     deleteLink.innerText = "x";  
  42.                       
  43.                     o.appendChild(deleteLink);  
  44.                     //把当前画出的对象加入到画布中   
  45.                     Rect.container.appendChild(o);  
  46.                     //处理onmousemove事件   
  47.                     Rect.container.onmousemove = Rect.move;  
  48.                     //处理onmouseup事件   
  49.                     Rect.container.onmouseup = Rect.end;  
  50.                 },  
  51.                 move: function(e){  
  52.                     var o = Rect.obj;  
  53.                     //dx,dy是鼠标移动的距离   
  54.                     var dx = Rect.getEvent(e).x - o.mouseBeginX;  
  55.                     var dy = Rect.getEvent(e).y - o.mouseBeginY;  
  56.                     //如果dx,dy <0,说明鼠标朝左上角移动,需要做特别的处理   
  57.                     if(dx<0){  
  58.                         o.style.left = Rect.getEvent(e).x;  
  59.                     }  
  60.                     if(dy<0){  
  61.                         o.style.top = Rect.getEvent(e).y;  
  62.                     }  
  63.                     o.style.height = Math.abs(dy);  
  64.                     o.style.width = Math.abs(dx);  
  65.                 },  
  66.                 end: function(e){  
  67.                             //onmouseup时释放onmousemove,onmouseup事件句柄   
  68.                     Rect.container.onmousemove = null;  
  69.                     Rect.container.onmouseup = null;  
  70.                     Rect.obj = null;  
  71.                 },  
  72.                 //辅助方法,处理IE和FF不同的事件模型   
  73.                 getEvent: function(e){  
  74.                                         if (typeof e == 'undefined'){  
  75.                                                 e = window.event;  
  76.                                         }  
  77.                                         //alert(e.x?e.x : e.layerX);   
  78.                                         if(typeof e.x == 'undefined'){  
  79.                                                 e.x = e.layerX;  
  80.                                         }  
  81.                                         if(typeof e.y == 'undefined'){  
  82.                                                 e.y = e.layerY;  
  83.                                         }  
  84.                                         return e;  
  85.                                 }  
  86.         };  
  87.      
  88.         </script>     
  89.     </head>     
  90.     <body>     
  91.             <div id="main" style="border: solid red 1px; height:90%; width:60%; curssor: default;"></div>  
  92.             增加一个按钮,可以关闭绘画效果,用来配合测试删除功能  
  93.             <input type="button" value="停止绘画" id="t"/>  
  94.     </body>     
  95.     <script type="text/javascript">   
  96.             //测试代码开始   
  97.             //初始化   
  98.             Rect.init("main");  
  99.               
  100.             document.getElementById('t').onclick = function(){  
  101.                     Rect.container.onmousedown = null;  
  102.             };  
  103.         </script>  
  104. </html>  

原创粉丝点击