Ir para o conteúdo

Linhas Com Alguras Iguais Em Grids


Criado por Rick =), Mai 09 2013 19:06

1 resposta neste tópico
  • Por favor, faça o login para responder

#1 Rick =)

Rick =)
  • Rick =)
  • Colaborador
  • 16 Revisões

Revisou 09 maio 2013 - 19:06

*
REVISÃO POPULAR!

/**
 * jQuery.fn.equelizeHeights(uniform)
 * If `uniform` is `true`, all elements will have the same height.
 * Otherwise, the equalization will be on a line-basis.
 *
 * Copyright (C) 2013 Henrique Barcelos 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
(function($){
	$.fn.equalizeHeights = function(uniform){
    	var elems = $(this);

    	var lineBasisEqualization = function() {
        	var maxHeight = 0;
        	var lineObjects = [];
        	var offsetTopFirst = elems.eq(0).offset().top;
        	elems.each(function(i){
            	if(offsetTopFirst != $(this).offset().top) {
                	$(lineObjects).height(maxHeight);
                	lineObjects = [this];
                	offsetTopFirst = $(this).offset().top;
                	maxHeight = 0;
            	} else {
                	if($(this).height() > maxHeight) {
                    	maxHeight=$(this).height();
                	}
                	lineObjects.push(this);
            	}
            	if(i == elems.size() - 1) {
                	$(lineObjects).height(maxHeight);
            	}
        	});
    	}

    	var uniformEqualization = function() {
        	var maxHeight = 0;
        	elems.each(function(i){
            	if($(this).height() > maxHeight) {
                	maxHeight=$(this).height();
            	}
        	});
        	elems.height(maxHeight);
    	}

    	if(uniform === true) {
        	uniformEqualization();
    	} else {
        	lineBasisEqualization();
    	}
    	return this;
	};
})(jQuery);

Uso:
// Nivela as alturas para cada "linha" distinta
$('ul li').equalizeHeights();

// Nivela todas as alturas para o mesmo tamanho
$('ul li').equalizeHeights(true);

Loading Module: Computer Engineer
|############-------------------| 50%

#2 Rick =)

Rick =)
  • Rick =)
  • Colaborador
  • 16 Revisões

Revisou 14 maio 2013 - 11:30

Bugfix:
* Havia um problema caso a última linha tivesse apenas uma célula.

/**
 * jQuery.fn.equelizeHeights(uniform)
 * If `uniform` is `true`, all elements will have the same height.
 * Otherwise, the equalization will be on a line-basis.
 *
 * Copyright (C) 2013 Henrique Barcelos 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
(function($){
	$.fn.equalizeHeights = function(uniform){
    	var elems = $(this);

    	var lineBasisEqualization = function() {
        	var maxHeight = undefined;
        	var lineObjects = [];
        	var offsetTopFirst = elems.eq(0).offset().top;
        	elems.each(function(i){
            	if(offsetTopFirst != $(this).offset().top) {
                	$(lineObjects).height(maxHeight);
                	lineObjects = [this];
                	offsetTopFirst = $(this).offset().top;
                	maxHeight = undefined;
            	} else {
                	if($(this).height() > maxHeight) {
                    	maxHeight=$(this).height();
                	}
                	lineObjects.push(this);
            	}
            	// For the last row an just if there are more than one element in the row
            	if(i == elems.size() - 1 && maxHeight !== undefined) {
                	$(lineObjects).height(maxHeight);
            	}
        	});
    	}

    	var uniformEqualization = function() {
        	var maxHeight = 0;
        	elems.each(function(i){
            	if($(this).height() > maxHeight) {
                	maxHeight=$(this).height();
            	}
        	});
        	elems.height(maxHeight);
    	}

    	if(uniform === true) {
        	uniformEqualization();
    	} else {
        	lineBasisEqualization();
    	}
    	return this;
	};
})(jQuery);

Loading Module: Computer Engineer
|############-------------------| 50%




1 usuário(s) está(ão) lendo este código

1 membro(s), 0 visitante(s) e 0 membros anônimo(s)