1

PHPから返されたjsonデータから約50枚の画像をロードしています。画像サイズを取得する PHP の余分な呼び出しを避けるために、クライアント側でこれを行いたいと思います。これは可能ですか?

イメージ リストを作成するための現在の jquery 呼び出しは次のとおりです。

// Create HTML for the images.
var html = '';

$.each(data.profiles, function() {

    // Can I determine the img height or do I need to wait until the image is loaded?

    html += '<a href="/view_profile.php?id='+this.user_id+'">';
    html += '<img src="'+this.file_name+'" width="200" height="" style="border:0;">';
    html += '</a><br>';

});
4

2 に答える 2

1

画像のロード イベントをバインドし、クライアントが次のように画像をロードしたときにそれを取得できます。

$.each(data.profiles, function() {

    // Can I determine the img height or do I need to wait until the image is loaded?

    html += '<a href="/view_profile.php?id='+this.user_id+'">';

    var image = new Image();  
    image.src = this.file_name;
    image.onload = function() {
        var height = image.height;
    };

    var img = $("<img/>").css('border', 0).prop('src', image.src);
    html += img.wrap('<div></div>').html();
    html += '</a><br>';
});
于 2012-08-10T16:54:46.273 に答える
0

これを試して:-

<img alt="photo-current" src="images/galleries/1/photo_1.jpg" id="photo-current"/>

<script type="text/javascript"> 
jQuery(document).ready(function($) {
      $("#photo-current").load(function() {
            var imageHeight = $(this).height();
            alert("image height " + imageHeight );
      });
});
</script>
于 2012-08-10T17:03:12.897 に答える