js 給div 添加選區,類似windows案頭選區一樣,相容ie6、7、8、9、10、Firefox、Google、所有瀏覽器

來源:互聯網
上載者:User

先來一張:

相容:

IE6 反應有點遲鈍,效果沒有Firefox好。

主要的效果就是,模仿windows案頭的那個選區。可以用選框選區表徵圖。

由於js代碼太多,只貼一段核心的代碼。

js核心代碼:

//選區function select(){//為工作區建立一個選區var _down=false;var pos={x:0,y:0};var move={x:0,y:0};$(".working").mousedown(function(event) {//如果是右鍵if(event.button==2)return;//不是點擊的案頭 返回if(!$(event.target).is(".icons"))return;_down=true;pos.x=event.clientX;pos.y=event.clientY;$(".select").css("left",pos.x);$(".select").css("top",pos.y);$(".select").show();});$(".working").mousemove(function(event) {if(!_down)return;move.x=event.clientX;move.y=event.clientY;//減去2 相容ievar width=move.x-pos.x-2;var height=move.y-pos.y-2;var select=$(".select");//選區向左if(width>=0&&height>=0){select.css("width",width);select.css("height",height);}else{//選區向右延伸if(width<0){//求絕對值var w=Math.abs(width);select.css("width",w);select.css("left",move.x);if(height>=0){select.css("height",height);}}if (height<=0) {var h=Math.abs(height);select.css("height",h);select.css("top",move.y);if(width>=0){select.css("width",width);}}}});$(".working").mouseup(function() {_down=false;$(".select").css("width",0);$(".select").css("height",0);$(".select").hide();});}

上面的效果當然還需要配合css 來做。完整的Dome過一段時間發。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.