I have a script that inserts images into different columns. I want the script to insert images into the column with the smallest height. However, every time I try to check the height of the columns, it always returns 30px (likely because the images are not loaded yet).
Below is the code snippet:
<div id="diaporama-col1" class="col-xs-4">
</div>
<div id="diaporama-col2" class="col-xs-4">
</div>
<div id="diaporama-col3" class="col-xs-4">
</div>
.
var listImgDiaporama=["resources/img/img-1.jpg", "resources/img/img-2.jpg", "resources/img/img-3.jpg", "resources/img/img-4.jpg", "resources/img/img-5.jpg", "resources/img/img-6.jpg", "resources/img/img-7.jpg", "resources/img/img-8.jpg", "resources/img/img-9.jpg", "resources/img/img-10.jpg", "resources/img/img-11.jpg"];
function addImgToDiaporama() {
for (i = 0; i < listImgDiaporama.length; i++) {
var col1Height = $("#diaporama-col1").height();
var col2Height = $("#diaporama-col2").height();
var col3Height = $("#diaporama-col3").height();
if (col1Height <= col2Height && col1Height <= col3Height) {
$("#diaporama-col1").append("<img src=\"" + listImgDiaporama[i] + "\"/>")
} else if (col2Height <= col1Height && col2Height <= col3Height) {
$("#diaporama-col2").append("<img src=\"" + listImgDiaporama[i] + "\"/>")
} else {
$("#diaporama-col3").append("<img src=\"" + listImgDiaporama[i] + "\"/>")
}
}
}
Is there a solution to make this functionality work correctly?