滚动

时间:2017-11-19 17:41:44

标签: javascript jquery html css twitter-bootstrap

我有一个缩小滚动的导航栏,navbar-nav中的li有一个图像,当导航栏缩小时,图像保持不变,但文本显示变为无。

我有一个函数,在它消失后显示此文本,但我有一个问题,我只希望此功能仅在导航栏缩小后工作,当导航栏转到它的正常大小时,我不希望这个功能工作,另一件事我希望功能分别显示每个li的文本,因为当我悬停它显示所有的文本跨度,我只想要李的孩子,我试过.children()方法,但它没有工作,请帮忙!这是我的代码:

    $(window).scroll(function() {
      if ($(document).scrollTop() > 50) {
        $('nav').addClass('shrink');
      } else {
        $('nav').removeClass('shrink');
      }
    });

$('.home').hover(function() {
	 $(this).css('margin-right', '5px');
   $('.navbar-nav li a span').css('display', 'block');
	 $('.navbar-nav li a').css('height', '155px');
	 $('.navbar-nav li a img').css('margin-right', '5px');
});
 body {
      padding-top: 50px;
      min-height:800px
    }

    nav a {
      padding-top: 20px !important;
      padding-bottom: 20px !important;
      font-size: 18px;
    }

    nav .navbar-toggle {
      margin: 13px 15px 13px 0;
      -moz-transition: all 0.3s;
      -webkit-transition: all 0.3s;
      transition: all 0.3s;	
    }

    .navbar-brand {
      font-size: 30px;
          -moz-transition: all 0.3s;
      -webkit-transition: all 0.3s;
      transition: all 0.3s;
    }

    nav.navbar.shrink {
      min-height: 35px;
        -moz-transition: all 0.3s;
      -webkit-transition: all 0.3s;
      transition: all 0.3s;	
    }

    nav.shrink a {
      padding-top: 10px !important;
      padding-bottom: 10px !important;
      font-size: 15px;
    }

    nav.shrink .navbar-brand {
      font-size: 25px;
    }

    nav.shrink .navbar-toggle {
      padding: 4px 5px;
      margin: 8px 15px 8px 0;
    }

    nav.shrink .navbar-nav li a span{
display: none
}
<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">

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Brand</a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav pull-right">
            <li class="active home"><a href="#"><img src="https://cdn0.iconfinder.com/data/icons/iconshock_guys/512/andrew.png" height="50" width="50"><span>Home</span></a></li>
            <li class="active service"><a href="#"><img src="https://cdn0.iconfinder.com/data/icons/iconshock_guys/512/andrew.png" height="50" width="50"><span>Services</span></a></li>
            <li class="active about"><a href="#"><img src="https://cdn0.iconfinder.com/data/icons/iconshock_guys/512/andrew.png" height="50" width="50"><span>About us</span></a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">
      <div class="text-center">
        <h1>Bootstrap starter template</h1>
        <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
      </div>
      <div class="text-center">
        <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
      </div>
      
    </div>

1 个答案:

答案 0 :(得分:1)

要实现你想要的行为,你不需要滥用javascript,当然它可以用JS来完成,但它更容易直接使用CSS

只需将此规则添加到您的代码中:

nav.shrink .navbar-nav li:hover a span{
  display: block
}

这当然只是示例,但我确信使用CSS方法可以更轻松,更快速地实现目标,而且不会产生额外的性能开销=)

这是一个工作片段:

&#13;
&#13;
    $(window).scroll(function() {
      if ($(document).scrollTop() > 50) {
        $('nav').addClass('shrink');
      } else {
        $('nav').removeClass('shrink');
      }
    });
&#13;
 body {
      padding-top: 50px;
      min-height:800px
    }

    nav a {
      padding-top: 20px !important;
      padding-bottom: 20px !important;
      font-size: 18px;
    }

    nav .navbar-toggle {
      margin: 13px 15px 13px 0;
      -moz-transition: all 0.3s;
      -webkit-transition: all 0.3s;
      transition: all 0.3s;	
    }

    .navbar-brand {
      font-size: 30px;
          -moz-transition: all 0.3s;
      -webkit-transition: all 0.3s;
      transition: all 0.3s;
    }

    nav.navbar.shrink {
      min-height: 35px;
        -moz-transition: all 0.3s;
      -webkit-transition: all 0.3s;
      transition: all 0.3s;	
    }

    nav.shrink a {
      padding-top: 10px !important;
      padding-bottom: 10px !important;
      font-size: 15px;
    }

    nav.shrink .navbar-brand {
      font-size: 25px;
    }

    nav.shrink .navbar-toggle {
      padding: 4px 5px;
      margin: 8px 15px 8px 0;
    }

    nav.shrink .navbar-nav li a span{
display: none
}
nav.shrink .navbar-nav li:hover a span{
display: block
}
&#13;
<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">

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Brand</a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav pull-right">
            <li class="active home"><a href="#"><img src="https://cdn0.iconfinder.com/data/icons/iconshock_guys/512/andrew.png" height="50" width="50"><span>Home</span></a></li>
            <li class="active service"><a href="#"><img src="https://cdn0.iconfinder.com/data/icons/iconshock_guys/512/andrew.png" height="50" width="50"><span>Services</span></a></li>
            <li class="active about"><a href="#"><img src="https://cdn0.iconfinder.com/data/icons/iconshock_guys/512/andrew.png" height="50" width="50"><span>About us</span></a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">
      <div class="text-center">
        <h1>Bootstrap starter template</h1>
        <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
      </div>
      <div class="text-center">
        <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
      </div>
      
    </div>
&#13;
&#13;
&#13;