首页 > 解决方案 > 如何将导航链接放在右侧并在其上应用 scrollspy?

问题描述

我正在制作一个投资组合,但我在使用 Bootstrap 4 时遇到了 2 个问题:

  1. 将导航栏链接放在右侧
  2. 而且,Bootstrap Scrollspy。

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

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

下面是我的代码:

代码:

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>

标签: htmlcsstwitter-bootstrap

解决方案


请试试这个代码

<!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>

推荐阅读