2017-08-03 67 views
1

我有一個JS代碼:更新元素高度時,調整窗口大小

var setPostHeaderHeightsInSlider = function() { 
    var titlesHeight = 0; 
    var teasersHeight = 0; 

    $('.posts-slider .post-item').each(function() { 
     var titleHeight = $(this).find('.post .post-header h3').height(); 
     var teaserHeight = $(this).find('.post .post-header p').height(); 

     if (titleHeight > titlesHeight) { 
      titlesHeight = titleHeight; 
      $('.posts-slider .post-item .post .post-header h3').css('height', titlesHeight); 
     } 

     if (teaserHeight > teasersHeight) { 
      teasersHeight = teaserHeight; 
      $('.posts-slider .post-item .post .post-header p').css('height', teasersHeight); 
     } 
    }); 
}; 

var onResize = function() { 
    console.log('resize'); 
    setPostHeaderHeightsInSlider(); 
}; 

$(document).ready(function() { 
    onResize(); 
    $(window).on('resize', function(){ 
     onResize(); 
    }); 
} 

我需要的元素更新的高度時,窗口大小調整,但我的代碼不能正常工作。

我在控制檯中看到文本,但高度不更新。

@edit:

我把我的html代碼。

HTML:

<div class="posts-slider swiper-container w-100 fl"> 
    <div class="swiper-wrapper"> 
     <div class="post-item swiper-slide ph4 pt3"> 
      <a href="artykul.html" class="w-100 fl"> 
       <article class="post w-100 fl"> 
        <figure class="post-photo w-100 tc fl ma0 relative"> 
         <img src="img/post_01.jpg" alt="Post 01" class="dib mw-100"> 
        </figure> 
        <header class="post-header w-100 tl fl"> 
         <h3 class="ma0 mt3">Lorem ipsum dolor sit amet, consectetur adipisicing elit</h3> 
         <p class="ma0 mt3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis, itaque, sapiente! Aliquid autem, deserunt dicta dolorum earum eius explicabo incidunt, modi natus temporibus ullam ut vel voluptatum? Libero recusandae, unde?</p> 
        </header> 
        <footer class="post-footer w-100 tl fl"> 
          <p class="pv3 ma0 mt4">22 czerwca 2017</p> 
        </footer> 
       </article> 
      </a> 
     </div> 
    </div> 
</div> 

高度設置僅在頁面刷新。

+0

您可以發佈您的html代碼? – vel

+0

發佈您的html代碼,是console.log打印? – Selvakumar

+0

我編輯了帖子。 –

回答

1

替換此行,它會爲你工作

var titleHeight = $(this).find('.post .post-header h3').removeAttr("style").height(); 
    var teaserHeight = $(this).find('.post .post-header p').removeAttr("style").height(); 
+0

感謝您的幫助! –

0

請嘗試刪除ph3
這意味着,
(通過採取代碼) $(this).find('.post .post-header').height();代替
$(this).find('.post .post-header p').height();

相關問題