简体   繁体   中英

Make biggest height all images

$(document).ready(function() {
            var enbuyuk = 0;
            var height = 0;
            $("#onecikanresim0").load(function() {
                enbuyuk = $(this).height();
            });

            var sinir = <?php echo count($urunler); ?>;
            for(var i=1; i<sinir; i++){
                $("#onecikanresim"+i).load(function() {
                    height = $(this).height();
                });
                if(height > enbuyuk){
                    enbuyuk = height;
                }
            }

            for(var i=0; i<sinir; i++){
                $("#onecikanresim"+i).src("height", enbuyuk);                   
            }
</script>

I want to all images to biggest height. But this code does not work. I think load works callback. But i dont know how to fix it. Can someone help me?

enbuyuk means biggest height

sinir means number of images

$("#onecikanresim"+i).src("height", enbuyuk);

Isn't the correct way to assign a height, I don't think .src() is even valid.

Try:

$("#onecikanresim"+i).css("height", enbuyuk);

This isn't your only issue. You're using enbuyuk before it will be assigned anything other than 0 . Everything after $("#onecikanresim0").load() will run before the callback.

It's hard to tell how you're expecting the logic to flow, but the below will get you closer. Honestly you'll probably want to refactor this whole thing because you're going to end up in callback hell. You don't necessarily need .load() . You can just check the height since your code is in the jQuery $(document).ready() and you can reasonably assume the image has loaded.

var enbuyuk = 0;
var height = 0;

$("#onecikanresim0").load(function() {
    enbuyuk = $(this).height();

    var sinir = <?php echo count($urunler); ?>;
    for(var i=1; i<sinir; i++){
        $("#onecikanresim"+i).load(function() {
            height = $(this).height();

            if(height > enbuyuk){
             enbuyuk = height;
            }
        });

    }

    for(var i=0; i<sinir; i++){
        $("#onecikanresim"+i).src("height", enbuyuk);                   
    }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM