My brain is cold) I get the size of the div $('.template').height()
and divide it by (temph) / 150
(150 is the size of the image.). But the size of the picture is specified in the form of 50% and it can change its size .
How do I get the size of the first image displayed? and template divided by the size of the picture.
It should be approximately like this: (temph) / imgsize
in the end for will scroll right even if the picture changes size .
$(window).load(function() { var temph = $('.template').height(); var res = (temph) / 150; var result = res.toFixed(); var theImages = ["http://www.planwallpaper.com/static/images/i-should-buy-a-boat.jpg","http://www.planwallpaper.com/static/images/butterfly-wallpaper.jpeg","http://www.planwallpaper.com/static/images/HD-Wallpapers1.jpeg"]; for (var i = 1; i <= result; i++) { var item = theImages[Math.floor(Math.random()*theImages.length)]; $('.template').append('<img src='+item+'><br>').hide().fadeIn(800); } }); </script>";
.template { background-color:green; width:100%; height:1000px; } .template img { width:50%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="template"></div>