固定导航菜单和移动响应

时间:2018-01-30 23:38:07

标签: html css bootstrap-4

我很难将导航菜单贴在顶部,并使用切换栏图标让移动设备响应。我正在使用Bootstrap 4.我尝试在div类中使用sticky-top和fixed-top,并尝试在我的标题CSS上修复位置,但它会中断。

这是我的代码:

header {
  border-bottom: 1px solid pink;
}

.nav {
  font-family: 'Lato', sans-serif;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 3px;
  padding: 5px;
}

.nav-link {
  color: black;
  margin-right: 20px;
  margin-left: 20px;
}

.dropdown-menu {
  font-family: 'Lato', sans-serif;
  font-size: 12px;
  text-transform: uppercase;
  text-align: center;
  letter-spacing: 3px;
  border: 0px;
}

.dropdown-menu-center {
  left: 50% !important;
  right: auto !important;
  transform: translate(-50%, 0) !important;
  margin-top: 30px;
}

.dropdown-menu a:hover {
  color: pink;
  background-color: white;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<!-- Nav menu -->
<header style="background-color:fff;">
  <div class="container">
    <div class="row justify-content-center">
      <nav>
        <ul class="nav">
          <li class="nav-item">
            <a class="nav-link" href="#">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Portfolio</a>
          </li>
          <div class="nav-item dropdown show">
            <a class="nav-link" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
							Services
						  </a>
            <div class="dropdown-menu dropdown-menu-center" aria-labelledby="dropdownMenuLink">
              <a class="dropdown-item" href="#">Wordpress Design</a>
              <a class="dropdown-item" href="#">Add-Ons</a>
              <a class="dropdown-item" href="#">Branding</a>
            </div>
          </div>
          <li class="nav-item">
            <a class="nav-link" href="#">How To Start A Blog</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Contact</a>
          </li>
        </ul>
      </nav>
    </div>
  </div>
</header>
<!-- End of nav menu -->
<div style="padding:1000px;">hi</div>

1 个答案:

答案 0 :(得分:1)

原因是:您没有加载所需的文件。

解决方案:用以下3个文件替换过时的jQuery:

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

编辑:

经过进一步检查,你几乎完全屠杀了导航栏。这是它无法正常工作的主要原因。

所以,这是一个带有居中链接的导航栏的完整实用示例。将其用作起始模板,然后逐行添加自定义css以避免破坏事物。另外,请注意使用mx-auto类,该类将项目水平居中(“mx”= margin-horizo​​ntal)。

这是工作代码:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light sticky-top">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mx-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Dropdown
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
        </ul>
    </div>
</nav>
<!-- End of nav menu -->
<div style="padding:1000px 0;">hi</div>