Search code examples
javascriptimageimage-scaling

Each image less than the previous


I have array with 5 images, which I displayed with PHP. How can I use JavaScript to diplay them such that each image is 10% lower than the previous?

foreach($top_images as $top_i) {
    echo '#'.$ratio;
    echo '
        <br><img src="'.$top_i['url'].'" ><br>
        <script type="text/javascript">
            $(document).ready(function(){
                var img = new Image();
                img.src = "'.$top_i['url'].'";
                img.onload = function() {
                    var width = this.width;
                    var height = this.height;
                    ratio = '.json_encode($ratio).';
                    this.width = this.width - this.width * ratio/10;
                    this.height = this.height - this.height * ratio/10;
                }
                });
        </script>
    ';
    $ratio++;
}

Solution

  • I wouldn't mix up php and js like you did. Just output the images normally with php and then add the js at the bottom of your site: http://jsfiddle.net/78hdpkzr/

    $(document).load(function() {
        lastHeight = 0;
        $("img").each(function(i) {
            if (i != 0) {
                $(this).css({
                    height: lastHeight / 100 * 90
                })
            }
            lastHeight = parseInt($(this).css('height'));
        })
    })