2015-06-25 38 views
0
$(".sl3-big-image").on('load', function() { 
     alturaImg3 = $('.sl3-big-image').height(); 
     $('.sl3-container').css('height',alturaImg3); 
     $('.sl3-container .sl2-container2').css('height',alturaImg3); 
    }); 

我认为.on('load',...)是根本不工作..有时img高度为0.我试过这个,但它也不工作。图像高度返回0使用。负载

$(".sl3-big-image").on('load', function() { 
      alturaImg3 = $('.sl3-big-image').height(); 
      $('.sl3-container').css('height',alturaImg3); 
      $('.sl3-container .sl2-container2').css('height',alturaImg3); 
     }).each(function() { 
      if(this.complete) $(this).load(); 
     }); 

可能是什么问题?谢谢

+0

我想你需要'$(本).height()' – Satpal

+0

的事情数可能是错误的。很难说没有看到你的HTML,甚至CSS(你可以有'img {height:0}'等等)。如果您使用Chrome浏览器进行测试,请尝试'naturalHeight'。 – alex

回答

1

在window.load函数内调用你的函数。

$(window).load(function(e){ 
    $(".sl3-big-image").on('load', function() { 
      alturaImg3 = $(this).height(); 
      $('.sl3-container').css('height',alturaImg3); 
      $('.sl3-container .sl2-container2').css('height',alturaImg3); 
     }).each(function() { 
      if(this.complete) $(this).load(); 
    }); 
}); 
+0

这完美的作品。谢谢! –

+0

@AbelAbad,很高兴为您效劳。不用谢。 – RGS

0

使用的this作品height属性就好了。

$image = $('img'); 
 

 
$image.on('load', function() { 
 
    console.log(this.height); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<img src="https://lh3.googleusercontent.com/-GEUSBr1DEWw/AAAAAAAAAAI/AAAAAAAAALg/28aKu1XOjeE/photo.jpg" alt="" />