2013-04-02 31 views
0

我怎樣才能修改這個插件,以便我可以通過我想要使用的高度計算類型。例如height()outerHeight()innerHeight()jquery通過高度,outHeight等選項

/*! 
* equalHeightColumns.js 1.0 
* 
* Copyright 2013, Paul Sprangers http://paulsprangers.com 
* Released under the WTFPL license 
* http://www.wtfpl.net 
* 
* Date: Thu Feb 21 20:11:00 2013 +0100 
*/ 
$.fn.equalHeightColumns = function(options) { 
    defaults = { 
     minWidth: -1,    // Won't resize unless window is wider than this value 
     maxWidth: 99999,   // Won't resize unless window is narrower than this value 
     setHeightOn: 'min-height', // The CSS attribute on which the equal height is set. Usually height or min-height 
     heightMethod: 'outerHeight' 
    }; 
    var $this = $(this); // store the object 
    options  = $.extend({}, defaults, options); // merge options 

    // Recalculate the distance to the top of the element to keep it centered 
    var resizeHeight = function(){ 

     // Get window width 
     var windowWidth = $(window).width(); 

     // Check to see if the current browser width falls within the set minWidth and maxWidth 
     if(options.minWidth < windowWidth && options.maxWidth > windowWidth){ 
      var height = 0; 
      var highest = 0; 

      // Reset heights 
      $this.css(options.setHeightOn, 0); 

      // Figure out the highest element 
      $this.each(function(){ 
       // height = $(this).height(); BEFORE 
       height = $(this).eval(options.heightMethod); 
       if(height > highest){ 
        highest = height; 
       } 
      }); 

      // Set that height on the element 
      $this.css(options.setHeightOn, highest); 
     } else { 
      // Add check so this doesn't have to happen everytime 
      $this.css(options.setHeightOn, 0); 
     } 
    }; 

    // Call once to set initially 
    resizeHeight(); 

    // Call on resize. Opera debounces their resize by default. 
    $(window).resize(resizeHeight); 
}; 

看到我已經添加$(this).eval(options.heightMethod);我不能得到正確的語法

+0

我打算設置此,使每個元素都可以通過heightMethod作爲數據屬性,但現在我只是想使用默認設置 –

+0

工作的基本知識只是做'$(本)[選項。 heightMethod]();'?確保驗證偶爾錯誤輸入的高度方法值,例如'Outer_Height'等 – dbf

+0

使用多個高度方法的選項已經存在,請查看defaults數組中的「heightMethod」。 – akluth

回答

1

如果你已經錯過了註釋的部分,

$(this)[options.heightMethod]();應該做的伎倆,儘管請確保您驗證偶數錯誤輸入的heightMethod值,例如Outer_Height等

通過簡單地檢查您要撥打的功能是否可以快速進行驗證。 outerHeight,存在。

if(typeof $(this)[options.heightMethod] != "undefined") 
    $(this)[options.heightMethod](); 
else 
    // some warning/error.