每张图片都比之前少

时间:2015-02-20 15:56:25

标签: javascript image image-scaling

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

2 个答案:

答案 0 :(得分:3)

我不会像你那样混淆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'));
    })
})

答案 1 :(得分:1)

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

唯一的问题是图像必须彼此相邻

CSS:

&#13;
&#13;
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;
}
&#13;
<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" />
&#13;
&#13;
&#13;