如果不是移动设备,则悬停时Bootstrap Navbar下拉列表

时间:2015-06-22 23:32:15

标签: javascript html css twitter-bootstrap

我正在尝试从导航栏创建引导下拉列表以显示在悬停上但停止在折叠(移动)上的悬停效果。这是我的想法,但当我添加if语句时,整个效果不起作用: HTML:

  <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 vaos" href="../index.html">VAOS</a>
      </div>
      <div id="navbar" class="collapse navbar-collapse navbar-right">
        <ul class="nav navbar-nav">
          <li class="active"><a class="trans" href="../index.html">Home</a></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">What is VAOS <span class="caret"></span></a>
            <ul class="dropdown-menu" role="menu">
              <li><a href="options.html">VAOS & Other Options</a></li>
              <li><a href="candidate.html">Am I a Candidate</a></li>
              <li><a href="surgeon.html">Find a Surgeon</a></li>
              <li><div class="ArrowUp1"></div></li>
            </ul>
         </li>

          <li><a href="experience.html">Testimonials</a></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Resources <span class="caret"></span></a>
            <ul class="dropdown-menu" role="menu">
              <li><a href="clinical-support.html">Clinical Support</a></li>
              <li><a href="videos.html">Procedural Videos</a></li>
              <li><a href="resources.html">Other Links & Resources</a></li>
              <li><div class="ArrowUp2"></div></li>
            </ul>
          </li>
        </ul>
      </div><!--/.nav-collapse -->
    </div>

  CSS:

@media (min-width: 768px) {
  .navbar-nav .open ul {
    display: none;
  }
  .navbar-default .navbar-nav > .open > a,
  .navbar-default .navbar-nav > .open > a:hover,
  .navbar-default .navbar-nav > .open > a:focus {
    color: #555;
    background: none;
  }
  .navbar-default .navbar-nav > li:hover {
    background: #e7e7e7;
  }
  .navbar-inverse .navbar-nav > .open > a,
  .navbar-inverse .navbar-nav > .open > a:hover,
  .navbar-inverse .navbar-nav > .open > a:focus {
    color: #969696;
    background: none;
  }
  .navbar-nav > li:hover {
    background: transparent !important;
  }
  .navbar-nav .hovernav:hover > .dropdown-menu {
    display: block;
  }
}

JavaScript的:

'use strict';

$('ul.nav li.dropdown').hover(function() {
  if(window.width > 767) {
    $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn(500);
    $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut(500);
  }   
});

2 个答案:

答案 0 :(得分:5)

您可以在没有任何脚本语言的情况下执行此操作。 试试吧。

&#13;
&#13;
@media screen and (min-width: 768px) {
  .dropdown:hover>.dropdown-menu {
    display: block;
  }
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/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.4/js/bootstrap.min.js"></script>
<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 vaos" href="../index.html">VAOS</a>

    </div>
    <div id="navbar" class="collapse navbar-collapse navbar-right">
      <ul class="nav navbar-nav">
        <li class="active"><a class="trans" href="../index.html">Home</a>
        </li>
        <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">What is VAOS <span class="caret"></span></a>

          <ul class="dropdown-menu" role="menu">
            <li><a href="options.html">VAOS & Other Options</a>
            </li>
            <li><a href="candidate.html">Am I a Candidate</a>
            </li>
            <li><a href="surgeon.html">Find a Surgeon</a>
            </li>
            <li>
              <div class="ArrowUp1"></div>
            </li>
          </ul>
        </li>
        <li><a href="experience.html">Testimonials</a>
        </li>
        <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Resources <span class="caret"></span></a>

          <ul class="dropdown-menu" role="menu">
            <li><a href="clinical-support.html">Clinical Support</a>
            </li>
            <li><a href="videos.html">Procedural Videos</a>
            </li>
            <li><a href="resources.html">Other Links & Resources</a>
            </li>
            <li>
              <div class="ArrowUp2"></div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <!--/.nav-collapse -->
  </div>
&#13;
&#13;
&#13;

它流畅而方便。如果它有帮助,请+1。

答案 1 :(得分:0)

将css悬停用于带有类下拉列表的项目,并将下拉菜单类设置为diplay块

@media screen and (min-width: 768px) {
    li.dropdown:hover .dropdown-menu{
            display:block;
        }
    }