gpt4 book ai didi

jquery - Bootstrap 4 scrollSpy 需要任何类型的 jQuery 代码来实现底部边框事件状态吗?

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:42 25 4
gpt4 key购买 nike

我很难在我的网站上实现 Bootstrap 4 scrollSpy,它具有事件状态的红色边框底部 onScroll 和/或 onClick。

每当我滚动到某个部分或单击它时,这就是我想要的:

Active State Border Bottom

这是我的 Codepen

使 Border Bottom 仅在 scrollSpy 和/或 onClick 上以其事件状态出现的 Bootstrap/CSS 唯一方法是什么?

我是 bootstrap 4 的新手,我仍在学习 jQuery 和/或 JS。请帮助我找到一个使用 bootstrap 或 css 的解决方案,或者至少使用几行 jQuery 的极简主义方法来解决这个问题。谢谢你!

CSS:

    body {
position: relative;
}

.navbar-nav .nav-link {
margin-right: 15px;
margin-left: 15px;
}

.navbar-nav li a{color:#d3d3d3;}

.navbar-nav li a:hover {color:#333;}

.navbar-expand-sm .navbar-nav a:hover:before {
border-bottom: 5px solid #fff;
bottom: -7px; width:auto;
content: " ";
left:18px;
position: absolute;
right:18px;
}

.navbar-expand-sm .navbar-nav .active a::after {
border-bottom: 5px solid red;
bottom: -7px; width:auto;
content: " ";
left:18px;
position: absolute;
right:18px;
}
.navbar-nav > li {
float: left;
position: relative;
}

.custom-toggler .navbar-toggler-icon {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255, 0, 0, 1)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
}

HTML:

<body data-spy="scroll" data-target=".navbar" data-offset="50">
<div class="wrapper">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<a class="navbar-brand" href="index.html">
<img src="/assets/brand/bootstrap-solid.svg" width="30" height="30" class="d-inline-block align-top" alt="">CMM
</a>

<button class="navbar-toggler custom-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-md-auto">
<li class="nav-item active">
<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>
</div>
</nav>

<div id="section1" class="container-fluid bg-success" style="padding-top:70px;padding-bottom:70px">
<h1>Section 1</h1>

</div>
<div id="section2" class="container-fluid bg-warning" style="padding-top:70px;padding-bottom:70px">
<h1>Section 2</h1>

</div>
<div id="section3" class="container-fluid bg-secondary" style="padding-top:70px;padding-bottom:70px">
<h1>Section 3</h1>

</div>
<div id="section41" class="container-fluid bg-danger" style="padding-top:70px;padding-bottom:70px">
<h1>Section 4 Submenu 1</h1>

</div>
<div id="section42" class="container-fluid bg-info" style="padding-top:70px;padding-bottom:70px">
<h1>Section 4 Submenu 2</h1>

</div>
</div>
</body>

最佳答案

你差不多好了,你只需要更正一个选择器,因为 active 类应用于 a 标记。所以你需要改变这个:

.navbar-expand-sm .navbar-nav .active a::after

通过这个:

.navbar-expand-sm .navbar-nav  a.active::after

body {
position: relative;
}

.navbar-nav .nav-link {
margin-right: 15px;
margin-left: 15px;
}

.navbar-nav li a {
color: #d3d3d3;
}

.navbar-nav li a:hover {
color: #333;
}

.navbar-expand-sm .navbar-nav a:hover:before {
border-bottom: 5px solid #fff;
bottom: -7px;
width: auto;
content: " ";
left: 18px;
position: absolute;
right: 18px;
}

.navbar-expand-sm .navbar-nav a.active::after {
border-bottom: 5px solid red;
bottom: -7px;
width: auto;
content: " ";
left: 18px;
position: absolute;
right: 18px;
}

.navbar-nav>li {
float: left;
position: relative;
}

.custom-toggler .navbar-toggler-icon {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255, 0, 0, 1)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
}
<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.0.0-beta.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
<style>
body {
position: relative;
}
</style>
</head>

<body data-spy="scroll" data-target=".navbar" data-offset="50">
<div class="wrapper">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<a class="navbar-brand" href="index.html">
<img src="/assets/brand/bootstrap-solid.svg" width="30" height="30" class="d-inline-block align-top" alt="">CMM
</a>

<button class="navbar-toggler custom-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-md-auto">
<li class="nav-item active">
<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>
</div>
</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>
</div>
</body>

关于jquery - Bootstrap 4 scrollSpy 需要任何类型的 jQuery 代码来实现底部边框事件状态吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48156191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com