Bootstrap透明导航栏模板中自定义JS文件嵌入位置咨询
Hey, let's fix this navigation bar issue step by step. You've got two main problems right now: incorrect placement of your JS file, and invalid HTML structure that's breaking your page rendering. Here's how to sort it out:
一、正确的JS文件引入位置
First, you need to load your bar.min.js right before the closing </body> tag. Also, since your navigation code depends on jQuery and Bootstrap's JS, you have to load those dependencies first. Let's break down the order:
- jQuery (required for both Bootstrap and your scroll effect)
- Bootstrap's JS + Popper.js (for the navbar collapse functionality)
- Your custom
bar.min.js
This ensures all the tools your code needs are already loaded when your script runs.
二、修正后的完整HTML代码
I've cleaned up your HTML and added the correct JS imports. Here's the full working version:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>hello</title> <!-- Custom CSS --> <link rel="stylesheet" href="main.css" /> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <!-- Ionicons for icons --> <link href="https://unpkg.com/ionicons@4.5.10-0/dist/css/ionicons.min.css" rel="stylesheet"/> </head> <body> <!-- Navigation Header --> <header class="header"> <nav class="navbar navbar-expand-lg fixed-top py-3"> <div class="container"> <a href="#" class="navbar-brand text-uppercase font-weight-bold">Transparent Nav</a> <button type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler navbar-toggler-right"> <i class="fa fa-bars"></i> </button> <div id="navbarSupportedContent" class="collapse navbar-collapse"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"><a href="#" class="nav-link text-uppercase font-weight-bold">Home <span class="sr-only">(current)</span></a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">About</a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">Gallery</a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">Portfolio</a></li> <li class="nav-item"><a href="#" class="nav-link text-uppercase font-weight-bold">Contact</a></li> </ul> </div> </div> </nav> </header> <!-- Page Content --> <div class="container"> <div class="pt-5 text-white"> <header class="py-5 mt-5"> <h1 class="display-4">Transparent Navbar</h1> <p class="lead mb-0">Using Bootstrap 4 and Javascript, create a transparent navbar which changes its style on scroll.</p> <p class="lead mb-0">Snippet by <a href="https://bootstrapious.com" class="text-white"> <u>Bootstrapious</u></a> </p> </header> <div class="py-5"> <p class="lead">Lorem ipsum dolor sit amet, <strong class="font-weight-bold">consectetur adipisicing </strong>elit. Explicabo consectetur odio voluptatum facere animi temporibus, distinctio tempore enim corporis quam <strong class="font-weight-bold">recusandae </strong>placeat! Voluptatum voluptate, ex modi illum quas nam distinctio.</p> <p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <div class="py-5"> <p class="lead">Lorem ipsum dolor sit amet, <strong class="font-weight-bold">consectetur adipisicing </strong>elit. Explicabo consectetur odio voluptatum facere animi temporibus, distinctio tempore enim corporis quam <strong class="font-weight-bold">recusandae </strong>placeat! Voluptatum voluptate, ex modi illum quas nam distinctio.</p> <p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididkkkkokokokokokorkgokrgokrgokrgokrgokrogkrogkorgkunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididkkkkokokokokokorkgokrgokrgokrgokrgokrogkrogkorgkunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> </div> </div> </div> <!-- JS Imports (order matters!) --> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> <!-- Your custom JS file --> <script src="assets/js/bar.min.js"></script> </body> </html>
三、关键修改点解释
- Fixed HTML Structure: I moved all your page content (header, main content) from the
<head>tag to the<body>tag. HTML requires that visible content lives inside<body>—putting it in<head>will break rendering. - JS Loading Order: As mentioned, dependencies first, then your custom script. This prevents "jQuery is undefined" errors.
- Path Check: Double-check that
assets/js/bar.min.jsis in the right location relative to your HTML file. If your HTML is in a subfolder (likepages/index.html), you'll need to adjust the path to../assets/js/bar.min.js.
四、Custom CSS Setup
Put your custom CSS into main.css (make sure the file exists in the same directory as your HTML, or adjust the link path if needed):
/* * * ========================================== * CUSTOM UTIL CLASSES * ========================================== * */ .navbar { transition: all 0.4s; } .navbar .nav-link { color: #fff; } .navbar .nav-link:hover, .navbar .nav-link:focus { color: #fff; text-decoration: none; } .navbar .navbar-brand { color: #fff; } /* Change navbar styling on scroll */ .navbar.active { background: #fff; box-shadow: 1px 2px 10px rgba(0, 0, 0, 0.1); } .navbar.active .nav-link { color: #555; } .navbar.active .nav-link:hover, .navbar.active .nav-link:focus { color: #555; text-decoration: none; } .navbar.active .navbar-brand { color: #555; } /* Change navbar styling on small viewports */ @media (max-width: 991.98px) { .navbar { background: #fff; } .navbar .navbar-brand, .navbar .nav-link { color: #555; } } /* * * ========================================== * FOR DEMO PURPOSES * ========================================== * */ .text-small { font-size: 0.9rem !important; } body { min-height: 110vh; background-color: #4ca1af; background-image: linear-gradient(135deg, #4ca1af 0%, #c4e0e5 100%); }
After making these changes, your navbar should work as expected, and the JS file will be loaded correctly.
内容的提问来源于stack exchange,提问作者SugarTe

