每张图片都比前一张少

Each image less than the previous

本文关键字:一张 张图片      更新时间:2024-03-06

我有一个带有5个图像的数组,我用PHP显示了这些图像。我如何使用JavaScript来平铺它们,使每个图像比前一个图像低10%?

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++;
}

我不会像你那样混淆php和js。只需使用php正常输出图像,然后在网站底部添加js: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'));
    })
})

使用CSS和下一个选择器(适用于所有浏览器)

唯一的问题是图像必须挨着

CSS:

img {
  width: 100px;
  float:left;
  clear:both;
}
img + img {
  width: 90px;
}
img + img + img {
  width: 80px;
}
img + img + img + img {
  width: 70px;
}
img + img + img + img + img {
  width: 60px;
}
<img src="http://placehold.it/100x100" />
<img src="http://placehold.it/100x100" />
<img src="http://placehold.it/100x100" />
<img src="http://placehold.it/100x100" />
<img src="http://placehold.it/100x100" />