2017-08-04 84 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元素高度。

即使您調整大小也將元素高度相同。如果您在%中提到元素高度,則可以在調整窗口大小時看到高度變化。

相關問題