Skip to content

Instantly share code, notes, and snippets.

@MFFunmaker
Last active January 31, 2017 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MFFunmaker/1f5f42f30c12780fa33030e6721a6463 to your computer and use it in GitHub Desktop.
Save MFFunmaker/1f5f42f30c12780fa33030e6721a6463 to your computer and use it in GitHub Desktop.
JS Height Calculate
var containerHeight = 0;
var outerContainerWidth = $('#outerContainer').width();
$('body').append('<div id="tempCloneItems" style="width:'+outerContainerWidth+'px;"></div>');
// iterate over the topic filter item containers
$('.heightAutoItemsThatHaveNoNumericalHeight').each(function(i,e){
// a little trick I learned in 'nam to get the computed height of any element
// regardless of the height being specified in CSS. Works everywhere.
var cloneHeight = $(e).clone().addClass('someTempClass').appendTo('#tempCloneItems').height();
containerHeight += cloneHeight;
$('.someTempClass').remove();
}).promise().done(function(){
$('#tempCloneItems').remove();
$('.heightAutoItemsThatHaveNoNumericalHeight').on('click', {'items_height': containerHeight}, function(e){
// when the items are clicked, toggle their height, as calculated via our hack
var itemsOuterContainer = $(this).data('itemsOuterContainer');
if ($('#'+itemsOuterContainer).hasClass('active')) {
$('#'+itemsOuterContainer).height(0);
} else {
$('#'+itemsOuterContainer).height(e.data.items_height);
}
});
});
@MFFunmaker
Copy link
Author

A little snippet/hack for calculating the height of items that do not ordinarily return a value when you try to grab their height via normal means.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment