【发布时间】:2021-07-09 21:19:17
【问题描述】:
所以我在网上看到一些模板,当您单击导航栏上的锚点时,页面会将您带到该部分,并且分区完全对齐,这意味着您在页面顶部看到的内容(显然忽略了navbar), 是分区的顶部。
我尝试这样做,但是当我尝试时,一些内容放在导航栏后面,有人知道如何解决这个问题吗?
我为“解决”这个问题所做的就是在部分块中添加填充,但如果它完全对齐会更好看。
这是模板:https://startbootstrap.com/theme/freelancer
模板的 HTML 和 JavaScript 如下所示(由于字符限制,我无法上传 CSS),有人可以告诉我代码的哪一部分是这样做的吗?
这是 HTML 的一部分:
**<!-- Navigation-->
<nav class="navbar navbar-expand-lg bg-secondary text-uppercase fixed-top" id="mainNav">
<div class="container">
<a class="navbar-brand" href="#page-top">Start Bootstrap</a>
<button class="navbar-toggler text-uppercase font-weight-bold bg-primary text-white rounded" type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
Menu
<i class="fas fa-bars"></i>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ms-auto">
<li class="nav-item mx-0 mx-lg-1"><a class="nav-link py-3 px-0 px-lg-3 rounded" href="#portfolio">Portfolio</a></li>
<li class="nav-item mx-0 mx-lg-1"><a class="nav-link py-3 px-0 px-lg-3 rounded" href="#about">About</a></li>
<li class="nav-item mx-0 mx-lg-1"><a class="nav-link py-3 px-0 px-lg-3 rounded" href="#contact">Contact</a></li>
</ul>
</div>
</div>
</nav>
<!-- Masthead-->
<header class="masthead bg-primary text-white text-center">
<div class="container d-flex align-items-center flex-column">
<!-- Masthead Avatar Image-->
<img class="masthead-avatar mb-5" src="assets/img/avataaars.svg" alt="..." />
<!-- Masthead Heading-->
<h1 class="masthead-heading text-uppercase mb-0">Start Bootstrap</h1>
<!-- Icon Divider-->
<div class="divider-custom divider-light">
<div class="divider-custom-line"></div>
<div class="divider-custom-icon"><i class="fas fa-star"></i></div>
<div class="divider-custom-line"></div>
</div>
<!-- Masthead Subheading-->
<p class="masthead-subheading font-weight-light mb-0">Graphic Artist - Web Designer - Illustrator</p>
</div>
</header>
<!-- About Section-->
<section class="page-section bg-primary text-white mb-0" id="about">
<div class="container">
<!-- About Section Heading-->
<h2 class="page-section-heading text-center text-uppercase text-white">About</h2>
<!-- Icon Divider-->
<div class="divider-custom divider-light">
<div class="divider-custom-line"></div>
<div class="divider-custom-icon"><i class="fas fa-star"></i></div>
<div class="divider-custom-line"></div>
</div>
<!-- About Section Content-->
<div class="row">
<div class="col-lg-4 ms-auto"><p class="lead">Freelancer is a free bootstrap theme created by Start Bootstrap. The download includes the complete source files including HTML, CSS, and JavaScript as well as optional SASS stylesheets for easy customization.</p></div>
<div class="col-lg-4 me-auto"><p class="lead">You can create your own custom avatar for the masthead, change the icon in the dividers, and add your email address to the contact form to make it fully functional!</p></div>
</div>
<!-- About Section Button-->
<div class="text-center mt-4">
<a class="btn btn-xl btn-outline-light" href="https://startbootstrap.com/theme/freelancer/">
<i class="fas fa-download me-2"></i>
Free Download!
</a>
</div>
</div>
</section>**
这就是 JavaScript
/*!
* Start Bootstrap - Freelancer v7.0.3 (https://startbootstrap.com/theme/freelancer)
* Copyright 2013-2021 Start Bootstrap
* Licensed under MIT (https://github.com/StartBootstrap/startbootstrap-freelancer/blob/master/LICENSE)
*/
//
// Scripts
//
window.addEventListener('DOMContentLoaded', event => {
// Navbar shrink function
var navbarShrink = function () {
const navbarCollapsible = document.body.querySelector('#mainNav');
if (!navbarCollapsible) {
return;
}
if (window.scrollY === 0) {
navbarCollapsible.classList.remove('navbar-shrink')
} else {
navbarCollapsible.classList.add('navbar-shrink')
}
};
// Shrink the navbar
navbarShrink();
// Shrink the navbar when page is scrolled
document.addEventListener('scroll', navbarShrink);
// Activate Bootstrap scrollspy on the main nav element
const mainNav = document.body.querySelector('#mainNav');
if (mainNav) {
new bootstrap.ScrollSpy(document.body, {
target: '#mainNav',
offset: 72,
});
};
// Collapse responsive navbar when toggler is visible
const navbarToggler = document.body.querySelector('.navbar-toggler');
const responsiveNavItems = [].slice.call(
document.querySelectorAll('#navbarResponsive .nav-link')
);
responsiveNavItems.map(function (responsiveNavItem) {
responsiveNavItem.addEventListener('click', () => {
if (window.getComputedStyle(navbarToggler).display !== 'none') {
navbarToggler.click();
}
});
});
});
这是模板的图片,我们可以看到,由于背景颜色,分区非常清晰:
当我点击导航栏上的投资组合时,它会将其向下移动,位于所选部门的顶部:
当我对我的 HTML 文件执行此操作时,它会将我带到选定的部分,但该部分的顶部被导航栏本身隐藏。这是该部分的图片:
这是我单击导航栏中的锚点后的图片,正如我们所见,它确实将我带到了该部分,但标题被导航栏本身隐藏,这在模板中不会发生。
为了解决这个问题,我只是在该部分中添加了填充,但它看起来仍然不如模板好
【问题讨论】:
-
你能添加一个有同样问题的sn-p吗?
-
我的文件很乱,因为我一直在从网上找到的不同模板中复制和粘贴,所以我不确定它是否会很有帮助。另外,我不知道问题出在 JavaScript、HTML 还是 CSS 中,您希望我向您展示哪个部分?
-
能否截取问题的截图或短视频?
-
好的,等一下
-
好的,我上传了一些截图,希望对他们有所帮助
标签: javascript html css navbar