Last active
January 31, 2017 23:39
-
-
Save MFFunmaker/1f5f42f30c12780fa33030e6721a6463 to your computer and use it in GitHub Desktop.
JS Height Calculate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.