2016-12-30 78 views
0

我能夠獲取和window.resize應用headerpadding topheightbody。問題是當我試圖調整window全寬的resize功能不工作,我認爲。應用頭部高度,體作爲填充頂部的窗口大小調整

var headerHeight = $('header').innerHeight(); 
 
console.log(headerHeight); 
 
function carouselHeight(){ 
 
\t $('body').removeAttr('style'); 
 
\t $('body').css("padding-top", headerHeight); 
 
}; 
 
$(document).ready(function(e) { 
 
\t carouselHeight();  
 
}); 
 
$(window).resize(function(){ 
 
\t carouselHeight(); 
 
});
body { 
 
    min-height: 2000px; 
 
    padding-top: 70px; 
 
}
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet"/> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<!-- Fixed navbar --> 
 
    <nav class="navbar navbar-default navbar-fixed-top"> 
 
     <div class="container"> 
 
     <div class="navbar-header"> 
 
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> 
 
      <span class="sr-only">Toggle navigation</span> 
 
      <span class="icon-bar"></span> 
 
      <span class="icon-bar"></span> 
 
      <span class="icon-bar"></span> 
 
      </button> 
 
      <a class="navbar-brand" href="#">Project name</a> 
 
     </div> 
 
     <div id="navbar" class="navbar-collapse collapse"> 
 
      <ul class="nav navbar-nav"> 
 
      <li class="active"><a href="#">Home</a></li> 
 
      <li><a href="#">Why Bootstrap</a></li> 
 
      <li><a href="#">Another action</a></li> 
 
       <li><a href="#">Something else here</a></li> 
 
      <li class="dropdown"> 
 
       <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a> 
 
       <ul class="dropdown-menu"> 
 
       <li><a href="#">Why Bootstrap</a></li> 
 
       <li><a href="#">Another action</a></li> 
 
       <li><a href="#">Something else here</a></li> 
 
       <li role="separator" class="divider"></li> 
 
       <li class="dropdown-header">Nav header</li> 
 
       <li><a href="#">Separated link</a></li> 
 
       <li><a href="#">One more separated link</a></li> 
 
       </ul> 
 
      </li> 
 
      </ul> 
 
      <ul class="nav navbar-nav navbar-right"> 
 
      <li><a href="../navbar/">Default</a></li> 
 
      <li><a href="../navbar-static-top/">Static top</a></li> 
 
      <li class="active"><a href="./">Fixed top <span class="sr-only">(current)</span></a></li> 
 
      </ul> 
 
     </div><!--/.nav-collapse --> 
 
     </div> 
 
    </nav> 
 

 
    <div class="container"> 
 

 
     <!-- Main component for a primary marketing message or call to action --> 
 
     <div class="jumbotron"> 
 
     <h1>Navbar example</h1> 
 
     <p>This example is a quick exercise to illustrate how the default, static and fixed to top navbar work. It includes the responsive CSS and HTML, so it also adapts to your viewport and device.</p> 
 
     <p>To see the difference between static and fixed top navbars, just scroll.</p> 
 
     <p> 
 
      <a class="btn btn-lg btn-primary" href="../../components/#navbar" role="button">View navbar docs &raquo;</a> 
 
     </p> 
 
     </div> 
 

 
    </div> <!-- /container -->

+0

似乎是一個非常複雜的方式來改變填充身體的基礎上的屏幕大小。最好讓CSS媒體查詢爲你做些繁重的工作:你知道每個視口大小需要填充什麼。只需製作一個媒體查詢,爲每個視口大小應用不同的填充。還要注意,在刪除樣式屬性時 - 您正在刪除該元素的所有CSS - 例如最小高度以及頂部填充。 – gavgrif

+0

什麼是你試圖在選擇器中使用的'''header'''? – aavrug

+0

@gavgrif是的,我們也可以通過媒體查詢來管理它。但在一個階段它絕對不會工作。所以我更喜歡劇本; –

回答

0

你必須這樣執行它們:

var headerHeight = $('header').innerHeight(); 
console.log(headerHeight); 
function carouselHeight(){ 
    $('body').removeAttr('style'); 
    $('body').css("padding-top", headerHeight); 
}; 

$(document).ready(carouselHeight); 
$(window).resize(carouselHeight); 
+0

謝謝你@kittyCat。問題是headerHeight需要放在side carouselHeight函數中。 –

1

對不起,我發現這個錯誤在我script。功能中沒有提到標題高度。所以它沒有得到正確的高度在調整大小

function carouselHeight(){ 
    var headerHeight = $('header').innerHeight(); 
    $('body').removeAttr('style'); 
    $('body').css("padding-top", headerHeight); 
    console.log(headerHeight); 
    console.log('hi'); 
}; 
$(document).ready(carouselHeight); 
$(window).resize(carouselHeight); 
相關問題