如何将导航链接放在右边并在其上应用scrollspy?

时间:2019-01-04 10:34:52

标签: html css twitter-bootstrap

我正在制作作品集,但是使用Bootstrap 4时遇到两个问题:

  1. 将导航栏链接置于右侧
  2. 然后是Bootstrap Scrollspy。

有人可以帮我解决上述2个问题吗?

我已经尝试过Bootstrap和w3schools网站上的代码,但是没有任何效果。

下面是我的代码:

CSS代码:

body {
    scroll-behavior: smooth;
    font-family: "Spectral", sans-serif;
    height: 100%;
    position: relative;
    background: linear-gradient(90deg, #ffffff 50%, #ffd600 50%);
}

nav ul li a {
    font-weight: bold;
    font-size: 20px;
    text-decoration: underline;
    text-underline-position: under !important;
    margin-right: 12px;
}

HTML代码:

<body data-spy="scroll" data-target="#mainNav" data-offset="0">
  <div class="main">
    <nav class="navbar navbar-expand-lg fixed-top" id="mainNav">
      <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">
          <li class="nav-item"><a class="nav-link" href="#homeSection">Home</a></li>
          <li class="nav-item"><a class="nav-link" href="#aboutSection">About</a></li>
          <li class="nav-item"><a class="nav-link" href="#skillSection">Skills</a></li>
          <li class="nav-item"><a class="nav-link" href="#workSection">Works</a></li>
          <li class="nav-item"><a class="nav-link" href="#contactSection">Contact</a></li>
        </ul>
      </div>
    </nav>

    <main id="fullpage">
      <section id="homeSection" class="section">.....</section>
      <section id="aboutSection" class="section">.....</section>
      <section id="skillSection" class="section">.....</section>
      <section id="workSection" class="section">.....</section>
      <section id="contactSection" class="section">.....</section>
    </main>
  </div>
</body>

1 个答案:

答案 0 :(得分:-1)

请尝试使用此代码

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
  <style>
  body {
      position: relative; 
  }
  </style>
</head>
<body data-spy="scroll" data-target=".navbar" data-offset="50">

<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">  
  <ul class="navbar-nav d-flex ml-auto">
    <li class="nav-item">
      <a class="nav-link" href="#section1">Section 1</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#section2">Section 2</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#section3">Section 3</a>
    </li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
        Section 4
      </a>
      <div class="dropdown-menu">
        <a class="dropdown-item" href="#section41">Link 1</a>
        <a class="dropdown-item" href="#section42">Link 2</a>
      </div>
    </li>
  </ul>
</nav>

<div id="section1" class="container-fluid bg-success" style="padding-top:70px;padding-bottom:70px">
  <h1>Section 1</h1>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="section2" class="container-fluid bg-warning" style="padding-top:70px;padding-bottom:70px">
  <h1>Section 2</h1>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="section3" class="container-fluid bg-secondary" style="padding-top:70px;padding-bottom:70px">
  <h1>Section 3</h1>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="section41" class="container-fluid bg-danger" style="padding-top:70px;padding-bottom:70px">
  <h1>Section 4 Submenu 1</h1>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="section42" class="container-fluid bg-info" style="padding-top:70px;padding-bottom:70px">
  <h1>Section 4 Submenu 2</h1>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
  <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>

</body>
</html>