为等时高度插件添加选项以延迟时间

时间:2011-08-26 12:20:25

标签: jquery plugins jquery-plugins

我想在equal height plugin添加一个新选项,允许在延迟一段时间后计算高度。原因是我有其他jquery脚本管理高度变化的图库。

$.fn.extend({        
    equalHeight: function (options) {
        //set default height to 0 or auto
        var defaults = {
            height:null,
            minHeight: 0,
            maxHeight: null     
        };
        //merge options
        options = $.extend(defaults, options);
        //cache the children (is this the parent or a group of elements)
        var children = (this.length > 1) ? this : this.children();             
        if(options.height !== null){
            //if specific height is set
            children.height(options.height);
        }else{
            //set the height to auto which releases the boxes heights
            children.css('height', 'auto');
            //loop though the elements and get their heights
            children.each(function () {            
                //if bigger than the default set to default
                if ($(this).height() > options.minHeight) options.minHeight= $(this).height();
                //if maxheight is set
                if(options.maxHeight !== null){
                    if(options.minHeight > options.maxHeight) options.minHeight= options.maxHeight;
                }
            });
            //set the height on all the children
            children.height(options.minHeight);
        }
        //return this so the jQuery chain is preserved
        return this;
    }
 });

1 个答案:

答案 0 :(得分:0)

可能你应该使用setInterval(100):

setInterval(100);    
var defaults = {
            height:null,
            minHeight: 0,
            maxHeight: null     
        };

或者,如果您可以捕获图像调整大小事件,请使用全局变量以确保调整图像大小和setInterval

---更新

你可以试试这个:

function func(obj, options) {
    //cache the children (is this the parent or a group of elements)
    var children = (obj.length > 1) ? obj: obj.children();             
    if(options.height !== null){
        //if specific height is set
        children.height(options.height);
    }else{
        //set the height to auto which releases the boxes heights
        children.css('height', 'auto');
        //loop though the elements and get their heights
        children.each(function () {            
            //if bigger than the default set to default
            if ($(obj).height() > options.minHeight) options.minHeight= $(obj).height();
            //if maxheight is set
            if(options.maxHeight !== null){
                if(options.minHeight > options.maxHeight) options.minHeight= options.maxHeight;
            }
        });
        //set the height on all the children
        children.height(options.minHeight);
    }    

    return obj;
}

//this closure reserves the $.
(function ($) {   
    $.fn.extend({        
        equalHeight: function (options) {
            //set default height to 0 or auto
            var defaults = {
                delay = false;
                delayCount = 0;
                height:null,
                minHeight: 0,
                maxHeight: null               
            };
            //merge options
            options = $.extend(defaults, options);

            //delay
            if (delay)
                return setTimeout(func, delayCount);           

        }
     });  
})(jQuery);

使用以下内容以延迟运行

$('#parent').equalHeight({ delay: true, delayCount: 100});