Skip to content

Instantly share code, notes, and snippets.

@RhythmShahriar
Created May 21, 2018 14:23
Show Gist options
  • Save RhythmShahriar/5e5997f170d827cef60735e095f267f0 to your computer and use it in GitHub Desktop.
Save RhythmShahriar/5e5997f170d827cef60735e095f267f0 to your computer and use it in GitHub Desktop.
This will equal height all grids as well as limit the title and also add (...) end of it
/**
* This will equal height all grids as well as
* limit the title and also add (...) end of it
*
* @author Rhythm Shahriar
* @version 1.0
*/
(function ($) {
//max equal heights parent and title
function equalizeHeights(parent, selector) {
// Find max height of all elements
var max = getHeight(parent);
//get min sized title
var minSizedTitle = getMinChar(selector);
//equal height parent
$(parent).each(function () {
$(this).css({
'min-height': max + 'px'
});
});
// Set all heights to max height
$(selector).each(function () {
var title = getWords(minSizedTitle, $(this).text()) + ' ...';
$(this).text(title);
});
}
//get max height
function getHeight(selector) {
var heights = new Array();
// Loop to get all element heights
$(selector).each(function () {
// Need to let sizes be whatever they want so no overflow on resize
$(this).css('min-height', '0');
$(this).css('max-height', 'none');
$(this).css('height', 'auto');
// Then add size (no units) to array
heights.push($(this).height());
});
// Find max height of all elements
return Math.max.apply(Math, heights);
}
//get min character
function getMinChar(selector) {
var chars = new Array();
$(selector).each(function () {
// Then add size (no units) to array
chars.push($(this).text().length);
});
return Math.min.apply(Math, chars);
}
//get min number of words within length
function getWords(limit, title) {
var words = title.replace(' ...', '').split(' ');
var lengthCounter = 0;
var sentence = '';
$.each(words, function (i, word) {
if (i === 0) {
sentence += word;
lengthCounter += word.length;
} else {
lengthCounter += word.length;
if (lengthCounter <= limit) {
sentence += ' ' + word;
}
}
});
return sentence;
}
/* ========= ADD SELECTORS HERE ============ */
//on load
$(window).load(function () {
// Fix heights on page load
equalizeHeights('#parent-node');
});
//on resize
$(window).resize(function () {
// Fix heights on page load
equalizeHeights('#parent-node .title');
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment