Source: http://www.cnblogs.com/walls/p/4256736.html
這篇文章總結的是我在使用resizable插件的過程中,遇到的問題及變通應用的奇思妙想。
一、resizable使用注意事項
以下是我在jsfiddle上寫的測試demo:http://jsfiddle.net/pLuymmp1
![](https://imageproxy.pixnet.cc/imgproxy?url=https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <div class="J_outer outer"> 2 <div class="J_inner inner"></div> 3 </div>
![](https://imageproxy.pixnet.cc/imgproxy?url=https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 .outer{width:100px;border:2px solid blue;} 2 .inner{width:50px;height:50px;border:2px solid green;margin:0 auto;}
![](https://imageproxy.pixnet.cc/imgproxy?url=https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 $(".J_outer").resizable({ 2 handles:"e" 3 }); 4 $(".J_inner").resizable({ 5 handles:"e" 6 }); 7 8 $(".J_outer").resizable("destroy");//解綁.J_outer的resizable 9 $(".J_outer").resizable({ 10 handles:"e" 11 }); 12 13 $(".J_inner").resizable("destroy");//解綁.J_inner的resizable 14 $(".J_inner").resizable({ 15 handles:"e" 16 });
兩個父子關系的div,對其綁定resizable和解綁,都是有次序之分的,否則會導致意外的結果發生
1. 正確的綁定順序:要先綁定父級的resizable方法,再綁定其子級的resizable方法
如果是,先綁定子級、再綁定父級,測試結果是:
子級和父級都綁定上了操作手柄,但是父級div的操作手柄無法響應拖拽事件,子級可以。
2.解綁父級的resizable,結果子級的resizable也被解綁
3.解綁子級的resizable,父級的正常未受影響
4.解綁子級的resizable,再重綁,子級和父級均可正常響應拖拽
5.解綁父級的resizable,再重綁,子級的resizable失效,父級的正常響應拖拽
所以,綁定的順序應為由父到子,解綁父級的resizable,則需要對其子級進行重綁resizable。
二、操作手柄的變通妙用
首先要明白的是,resizable綁定的手柄最多只能有e,s,w,n,se,sw,ne,nw八個方向的八個手柄,每個方向有且只能有一個。
那問題來了,如果我想要在s方向有兩個手柄,應該要怎么辦?
我在項目中就遇到這個問題,最后是以取巧的方式做到的。具體的方法是:再添加其余七個方向中的一個,然后在create的時候,將其特有的手柄class置換為ui-resizable-s,就可以了。
demo地址:http://jsfiddle.net/q58chj0h/
當時解決這個問題,心里別提多開心,哈哈
三、resizable存在的bug
jquery.ui.resizable aspectRatio在init后無法進行重新設置
解決方法:
修復代碼:
1 var oldSetOption = $.ui.resizable.prototype._setOption; 2 $.ui.resizable.prototype._setOption = function(key, value) { 3 oldSetOption.apply(this, arguments); 4 if (key === "aspectRatio") { 5 this._aspectRatio = !!value; 6 } 7 };
這樣就可以進行實時切換等比例和非等比例拉伸了,順便貼一下我的等比例拉伸實現代碼:
//用于識別是否按等比例調整大小 $sw.mouseover(function(){ $self.attr("aspectRatio", "1"); }); $se.mouseover(function(){ $self.attr("aspectRatio", "1"); }); $ne.mouseover(function(){ $self.attr("aspectRatio", "1"); }); $nw.mouseover(function(){ $self.attr("aspectRatio", "1"); }); $n.mouseover(function(){ $self.attr("aspectRatio", "0"); }); $e.mouseover(function(){ $self.attr("aspectRatio", "0"); }); $s.mouseover(function(){ $self.attr("aspectRatio", "0"); }); $w.mouseover(function(){ $self.attr("aspectRatio", "0"); });
1 start:function(event,ui){ 2 //判斷是否按等比例進行調整 3 if($(this).attr("aspectRatio") == "1"){ 4 $(this).resizable("option", "aspectRatio", true); 5 }else{ 6 $(this).resizable("option", "aspectRatio", false); 7 } 8 }
via:cnblogs.com/walls/p/4256736.html
![]() |
不含病毒。www.avast.com |