Skip to content

Instantly share code, notes, and snippets.

@rezozero
Created November 6, 2012 18:32
Show Gist options
  • Select an option

  • Save rezozero/4026578 to your computer and use it in GitHub Desktop.

Select an option

Save rezozero/4026578 to your computer and use it in GitHub Desktop.
JS: makeImagesInRow
/*
* Compute collection images height in order to make them enter in a given width
* We need to know default images height and the given final width
*/
function computeDeployedArticleImages ( article )
{
var computedWidth = 0;
/* We get the first image height */
var startHeight = $(article).find(".img_container img").first().actual("outerHeight", {includeMargin:false});
/* We apply this height to every image before computing global width*/
$(article).find(".img_container img").css("height", startHeight+"px");
/* We set to wanted global width */
var endWidth = $(article).find(".img_container").actual("outerWidth", {includeMargin:false});
if ($(article).find(".img_container img").length > 1) {
/* Loop to compute real global width */
$(article).find(".img_container img").each(function (index) {
computedWidth += $(this).actual("outerWidth",{includeMargin:true});
});
/* Get the ratio between Real width and Wanted width */
var ratio = computedWidth/endWidth;
/* Apply this ratio to get the final height */
var endHeight = Math.floor(startHeight/ratio);
$(article).find(".img_container img").css("height", endHeight+"px");
}
else {
var unique_img = $(article).find(".img_container img").get(0);
$(article).find(".img_container").css("height", $(unique_img).actual("outerHeight", {includeMargin:false})+"px");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment