javascript 繪製矩形框_javascript技巧

來源:互聯網
上載者:User
如果不考慮把整個代碼封裝起來, 大約5分鐘裡也可以寫出類似的效果,為了把整個代碼封裝成一個對象,稍微借鑒了Pro Javascript Techniques書中的代例子的風格。
Pro Javascript Techniques書中推薦的drag and drop 例子:http://boring.youngpup.net/2001/domdrag
<html> <head> <title>Rect</title> <style type="text/css"> </style> <script type="text/javascript"> var Rect = { //當前正在畫的矩形對象 obj: null, //畫布 container: null, //初始化函數 init: function(containerId){ Rect.container = document.getElementById(containerId); if(Rect.container){ //滑鼠按下時開始畫 Rect.container.onmousedown = Rect.start; } else{ alert('You should specify a valid container!'); } }, start: function(e){ var o = Rect.obj = document.createElement('div'); o.style.position = "absolute"; // mouseBeginX,mouseBeginY是輔助變數,記錄下滑鼠按下時的位置 o.style.left = o.mouseBeginX = Rect.getEvent(e).x; o.style.top = o.mouseBeginY = Rect.getEvent(e).y; o.style.height = 0; o.style.width = 0; o.style.border = "dotted black 1px"; //向o添加一個叉叉,點擊叉叉可以刪除這個矩形 var deleteLink = document.createElement('a'); deleteLink.href="#"; deleteLink.onclick = function(){ Rect.container.removeChild(this.parentNode); //this.parentNode.style.display = "none"; //alert(this.tagName); } deleteLink.innerText = "x"; o.appendChild(deleteLink); //把當前畫出的對象加入到畫布中 Rect.container.appendChild(o); //處理onmousemove事件 Rect.container.onmousemove = Rect.move; //處理onmouseup事件 Rect.container.onmouseup = Rect.end; }, move: function(e){ var o = Rect.obj; //dx,dy是滑鼠移動的距離 var dx = Rect.getEvent(e).x - o.mouseBeginX; var dy = Rect.getEvent(e).y - o.mouseBeginY; //如果dx,dy <0,說明滑鼠朝左上方移動,需要做特別的處理 if(dx<0){ o.style.left = Rect.getEvent(e).x; } if(dy<0){ o.style.top = Rect.getEvent(e).y; } o.style.height = Math.abs(dy); o.style.width = Math.abs(dx); }, end: function(e){ //onmouseup時釋放onmousemove,onmouseup事件控制代碼 Rect.container.onmousemove = null; Rect.container.onmouseup = null; Rect.obj = null; }, //輔助方法,處理IE和FF不同的事件模型 getEvent: function(e){ if (typeof e == 'undefined'){ e = window.event; } //alert(e.x?e.x : e.layerX); if(typeof e.x == 'undefined'){ e.x = e.layerX; } if(typeof e.y == 'undefined'){ e.y = e.layerY; } return e; } }; </script> </head> <body> <div id="main" ></div> 增加一個按鈕,可以關閉繪畫效果,用來配合測試刪除功能 <input type="button" value="停止繪畫" id="t"/> </body> <script type="text/javascript"> //測試代碼開始 //初始化 Rect.init("main"); document.getElementById('t').onclick = function(){ Rect.container.onmousedown = null; }; </script> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.