简体   繁体   English

使所有图像最大高度

[英]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. 我认为load可以回调。 But i dont know how to fix it. 但是我不知道如何解决它。 Can someone help me? 有人能帮我吗?

enbuyuk means biggest height enbuyuk意味着最大的身高

sinir means number of images sinir表示图像数量

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

Isn't the correct way to assign a height, I don't think .src() is even valid. 这不是分配高度的正确方法,我认为.src()甚至无效。

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 . 在使用enbuyuk之前,您将被分配0以外的任何0 Everything after $("#onecikanresim0").load() will run before the callback. $("#onecikanresim0").load()都将在回调之前运行。

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() . 您不一定需要.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. 您可以只检查高度,因为您的代码位于jQuery $(document).ready()并且可以合理地假定图像已加载。

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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