2017-08-04 82 views
0

我想获得窗口调整大小的div标记的高度,但我总是获得相同的高度。正确触发事件大小调整。jQuery的窗口调整大小获取元素的高度

var resizeNews = this.resizeNews = function() { 
    $('.top-news__pillar').each(function (index){ 
      console.log($(this).outerHeight()); 
      if($(this).outerHeight() > maxHeightPillar) maxHeightPillar = $(this).outerHeight(); 
     });   

/如果我不设置高度以下的高度调整/

 $('.top-news__pillar').each(function (index){ 
      $(this).outerHeight(maxHeightPillar); 
     }); 
    } 

    $(window).on('resize',resizeNews); 
+0

那么,当窗口大小调整时,通常元素会改变它们的宽度......你为什么期望它们改变高度? – MysterX

+0

是否有'top-news__pillar'的CSS'height'? – tech2017

+0

上面的代码对我来说工作正常,添加正确的代码来重现问题。 –

回答

0

尝试时的.outerHeight()代替.height()

+0

如果'高度'不改变如何'outerHeight'将解决问题? – Durga

0

试试这个代码正确更改。

$(document).ready(function() { 
     $(window).resize(function() { 
      var bodyheight = $('.top-news__pillar').height(); 
      console.log(bodyheight); 
     }).resize(); 
    }); 
0

是的,你会得到永远相同的高度,为什么,因为如果你指定一个像px,而不是像`%响应单元在固定单元的top-news__pillar元素高度。

即使您调整大小也将元素高度相同。如果您在%中提到元素高度,则可以在调整窗口大小时看到高度变化。

相关问题