导航栏缩小时修复导航栏元素的大小

时间:2017-04-17 18:29:46

标签: javascript jquery html css twitter-bootstrap

我有一个固定顶部的导航栏,我做了一个功能,使它在滚动缩小但我有一个问题,导航栏中的ul类没有正确收缩,我给它一个行高度,以垂直对齐它navbar-brand但我无法弄清楚当导航栏缩小时如何修复,这是我的代码:

$(document).ready(function(){
  var scrollTop = 0;
  $(window).scroll(function(){
    scrollTop = $(window).scrollTop();
     $('.counter').html(scrollTop);
    
    if (scrollTop >= 100) {
      $('#global-nav').addClass('scrolled-nav');
    } else if (scrollTop < 100) {
      $('#global-nav').removeClass('scrolled-nav');
    } 
    
  }); 
  
});
#global-nav{
height: 150px;
  -webkit-transition: height .5s, line-height .5s;
  transition: height .5s, line-height .5s;
}

.navbar-brand img{
height:95px;
}

.navbar-default .navbar-nav>li>a{
   line-height: 95px;
   }
   
   .navbar-brand
  img{
   display: inline-block;
   -webkit-transition: all .5s;
   transition: all .5s;
}
.scrolled-nav .navbar-brand img{
  height: 50px;
}
.scrolled-nav{
  height: 100px !important;
  line-height: 100px !important;
}
.counter{
  width: 20px;
  height: 20px;
  background: black;
  color: #fff;
  position: fixed;
  top: 120px;
  right: 0;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<nav id="global-nav" class="navbar navbar-default navbar-custom navbar-fixed-top">
   <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <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 page-scroll" href="#page-top"><img src="http://placehold.it/350x150"></a>
            </div>
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right">
                    <li class="hidden">
                        <a href="#"></a>
                    </li>
                    <li class="active"> <a href="#">LINK 1</a> </li>
                    <li> <a href="#">LINK 2</a> </li>
            
                </ul>
            </div>
        </div>
 </nav>
 
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

2 个答案:

答案 0 :(得分:1)

您的问题是

.navbar-default .navbar-nav>li>a {
  line-height: 95px;
}

当你考虑填充时,你需要70px为小.navbar-default.scrolled-nav .navbar-nav>li>a { line-height: 70px; } (或小时删除填充)。

简单回答:

function generateMarkup(handValue) {
    if (handValue >= 0.9) {
        return document.getElementById("resultText").innerHTML = "You can play any two cards from this position.";
    }

    if (handValue < 0.06) {
        return document.getElementById("resultText").innerHTML = '<tr><td class="tooltip"><img src="' + startHand[0] + '.jpg"><span class="tooltiptext">' + toolTips[0] + '</span></td>';   
    }

    if (handValue < 0.20) {
        return document.getElementById("resultText").innerHTML = '<tr><td class="tooltip"><img src="' + startHand[0] + '.jpg"><span class="tooltiptext">' + toolTips[0] + '</span></td><td class="tooltip">' + startHand[1] + '.jpg"><span class="tooltiptext">' + toolTips[1] + '</span></td>';
    }

    if (handValue < 0.40) {
        return document.getElementById("resultText").innerHTML = '<tr><td class="tooltip"><img src="' + startHand[0] + '.jpg"><span class="tooltiptext">' + toolTips[0] + '</span></td><td class="tooltip">' + startHand[1] + '.jpg"><span class="tooltiptext">' + toolTips[1] + '</span></td><td class="tooltip">' + startHand[2] + '.jpg"><span class="tooltiptext">' + toolTips[2] + '</span></td>';
    }

    return document.getElementById("resultText").innerHTML = 'You should play if your cards are in one of the following categories:<br><br><table>';
}

答案 1 :(得分:0)

使用代码片段进行复制很棘手,但控制导航项高度的CSS是这样的:

.navbar-default .navbar-nav>li>a {
    line-height: 95px;
}

因为此代码使用行高来使文本在导航按钮中居中,所以这是调整缩小高度所需的属性。

或者,您可以将行高与收缩导航分开并使用padding-toppadding-bottom,并在添加滚动类时调整这些属性。