`
zhizedai
  • 浏览: 16030 次
  • 性别: Icon_minigender_1
  • 来自: 大连
最近访客 更多访客>>
社区版块
存档分类
最新评论

js 画矩形程序(按下ctrl或者shift键开始draw,在按一下停止)

    博客分类:
  • js
阅读更多
<HTML>  
<HEAD>  
<TITLE>rect   sample</TITLE>  
<div   id="rect"   style="position:absolute;left:0px;top:0px;width:0;height:0;border-top: 2px solid    red;border-left:   2px   solid    red;border-right:   2px   solid    red;border-bottom:   2px   solid    red;z-index:100000px;">  
</div>
<script   type="text/javascript">  
  var   startX,startY,endX,endY;  
  var   dragFlag; 
  var   startDraw = false;
  function   doMouseDown(event){
  event = window.event||event; 
  dragFlag   =   0;  
  if(!startDraw) return ;
  startX   =   event.clientX;  
  startY   =   event.clientY;  
  document.getElementById("rect").style.left   =   startX+"px";  
  document.getElementById("rect").style.top   =   startY+"px";  
  document.getElementById("rect").style.height   =   "0px";  
  document.getElementById("rect").style.width   =   "0px";  
  }      
  function   doMouseUp(){  
  dragFlag   = (dragFlag==0)?1:0; 
  startX=startY=endX=endY=0;
  }      
  function   doMouseMove(event){  
  if(!startDraw) return ;
  event = window.event||event;
  if(dragFlag   ==   0){  
  endX   =   event.clientX;  
  endY   =   event.clientY;  
  nowHeight   =   endY   -   startY;  
  nowWidth   =   endX   -   startX;      
  if(nowHeight<0){  
  document.getElementById("rect").style.top   =   endY+"px";  
  nowHeight   =   -1*nowHeight;  
  }  
  if(nowWidth<0){  
  document.getElementById("rect").style.left   =   endX+"px";  
  nowWidth   =   -1*nowWidth;  
  }      
  document.getElementById("rect").style.height   =   nowHeight+"px";  
  document.getElementById("rect").style.width   =   nowWidth+"px";  
  }  
  }  
  document.onmousemove   =   doMouseMove;  
  document.onmousedown   =   doMouseDown;  
  document.onmouseup   =   doMouseUp;  

  document.onkeydown = function(e){
   var e = e || event;
   var code = e.keyCode || e.which || e.charCode;
   if(code== 16 || code==17){
    startDraw=(startDraw)?false:true;
   }
}
  </script>  
  </HEAD>     
  <BODY> 
    <span>按下ctrl或者shift键开始draw,在按一下停止</span> 
  </BODY>  
  </HTML>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics