2016-12-30 41 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); 
相关问题