0

我怎样才能修改这个插件,以便我可以传递我想使用的高度计算类型。例如height()outerHeight()innerHeight()

/*! 
* 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);我无法正确获取语法

4

1 回答 1

1

如果你错过了评论,

$(this)[options.heightMethod]();应该可以解决问题,但请确保验证 heightMethod 的值是否存在偶尔的错误输入,例如 Outer_Height 等。

只需检查您正在调用的函数是否outerHeight存在,即可快速完成验证。

if(typeof $(this)[options.heightMethod] != "undefined")
  $(this)[options.heightMethod]();
else
  // some warning / error.
于 2013-04-02T13:16:30.650 回答