2013-10-10 225 views
0

我正在嘗試構建一個像jquery插件的任務欄。我正試圖在div中添加按鈕。當我有幾個可以放入父div的按鈕時,可以將按鈕大小設置爲默認按鈕大小。當新的按鈕要添加父div開始溢出,然後按鈕大小應調整大小,以適應父母。當它們再次被移除時,按鈕應該再次增加到它們的默認大小。這裏是我的代碼用jQuery隨時更改CSS顯示屬性並應用更改

_resizeChildren: function() { 

     var self = this; // this is the <div> holding the taskbar 
     var children = self.element.children(); 
     var len = children.length; 
     var toolbarWidth = parseInt(self.element.width()); 
     var totalMargin = parseInt(children.css('marginLeft')) + parseInt(children.css('marginRight')); 


     if (toolbarWidth/(len + totalMargin) >= $('#master').outerWidth()) //#master is a <div> holding the default size 
     { 
      self.element.css({'display': 'block'}); 
      self.element.css({'tableLayout': ''}); 
      children.css({'display': ''}); 
      children.outerWidth(parseInt($('#master').outerWidth())); 
     } 
     else { 
      self.element.css({'display': 'table'}); 
      self.element.css({'tableLayout': 'fixed'}); 
      children.css({'display': 'table-cell'}); 

     } 
    }, 

的問題是,當我運行這段代碼,當新的按鈕添加多達父div的寬度他們是單位的微細使用默認大小。當它們開始溢出時,舊的按鈕不會調整大小,因此留下很小的空間用於在非常薄的空間中向右擠壓的新按鈕。

+2

你能提供一個http://jsfiddle.net鏈接 – shubendrak

+0

那麼,如果你不能複製,我們也不能。不幸的是,我們在這種情況下幫不了多少忙 –

回答

0

所以我終於找到了一個解決方案。如果/ else塊應該是

_resizeChildren: function() { 

     var self = this; // this is the <div> holding the taskbar 
     var children = self.element.children(); 
     var len = children.length; 
     var toolbarWidth = parseInt(self.element.width()); 
     var totalMargin = parseInt(children.css('marginLeft')) + parseInt(children.css('marginRight')); 


     if (toolbarWidth/(len + len * totalMargin) >= parseInt($('#master').outerWidth())) //#master is a <div> holding the default size 
     { 
      self.element.css({'display': 'block'}); 
      self.element.css({'tableLayout': ''}); 
      children.css({'display': ''}); 
      var maxWidth =$('#master').outerWidth(); 
      children.css('maxWidth', maxWidth); 
     } 
     else { 
      self.element.css({'display': 'table'}); 
      self.element.css({'tableLayout': 'fixed'}); 
      children.css({'display': 'table-cell'}); 
      children.css('maxWidth', ''); 

     } 
    }, 

現在,當我嘗試應用這些性質的一次只使用一個調用的.css(),他們開始發生各種討人喜歡的東西,如任務欄調整大小,按鈕消失等我沒有太注意它。它的工作原理是這樣的,我認爲它沒問題。