Equal height columns in jQuery

夙愿已清 提交于 2019-12-03 08:58:15

Right, thought that might be useful so made it in to a jQuery plugin for you.

Demo: http://jsfiddle.net/AXqBb/

equalizer.js:

(function($) {
    String.prototype.capitalize = function() {
        return this.replace(/^(.)/, function (c) { return c.toUpperCase(); })
    };

    $.fn.equalize = function(hw) {
        if (!hw) {
            hw = 'height';
        }

        var max = 0;
        var prop = (typeof document.body.style.maxHeight != 'undefined' ? 'min' + hw.capitalize() : hw);
        var offset = 'offset' + hw.capitalize();

        this.each(function() {
            var calc = parseInt(this[offset]);
            if (calc > max) {
                max = calc;
            }
        });

        this.each(function() {
            $(this).css(prop, max - (parseInt(this[offset]) - $(this)[hw]()));
        });

        return max;
    };
})(jQuery);

Called like this:

var maxHeight = $('.sizeMe').equalize('height');

I have kept the code as similar to what you posted as possible, so you can see the changes, so apologise for any bad style - hopefully it is attributable towards the original author. ;-)

NB. I added a basic first-word capitalisation function to String within this code; if already defined that would need to be removed.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!