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

10-07 19:35
查看更多