Добавить опцию для плагина равной высоты для задержки - PullRequest
1 голос
/ 26 августа 2011

Я хочу добавить в плагин равную высоту новую опцию, которая позволяет рассчитывать высоту после некоторой задержки. Причина в том, что у меня есть другой скрипт 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 голосов
/ 26 августа 2011

Может быть, вам следует использовать 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});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...