I have a page with one fixed-top navbar. Then I have 3 sections. In 2nd section I have nav-tabs. Upon scrolling into the 2nd section, I want the nav-tabs navbar to stick to top, below my fixed-top navbar. When user has scrolled out of the 2nd section, the nav-tabs navbar should disappear and only the fixed-top navbar must remain.
I can not find any tutorials or guidance related to what I'm trying to achieve. Any idea how to do this in bootstrap 5 using jquery?
index.html
<nav class="navbar navbar-expand-lg fixed-top">
<div class="container-fluid">
<a class="navbar-brand m-1" href="#intro">Tarheeb EMS Presentation</a>
<button class="navbar-toggler collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#navbarMain"
aria-controls="navbarMain" aria-expanded="false" aria-label="Toggle navigation">
<span></span>
<span></span>
<span></span>
</button>
<div class="collapse navbar-collapse" id="navbarMain">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="#">Introduction</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Platforms</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Comparison</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Summary</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</li>
</ul>
</div>
</nav>
<section class="1">
....
</section>
<section class="2">
<nav>
<div class="nav nav-tabs sticky" id="nav-tab" role="tablist">
<button class="nav-link active" id="nav-cvent-tab" data-bs-toggle="tab" data-bs-target="#nav-cvent"
type="button" role="tab" aria-controls="nav-contact" aria-selected="false">Cvent</button>
<button class="nav-link active" id="nav-cvent-tab" data-bs-toggle="tab" data-bs-target="#nav-cvent"
type="button" role="tab" aria-controls="nav-contact" aria-selected="false">Cvent</button>
<button class="nav-link active" id="nav-cvent-tab" data-bs-toggle="tab" data-bs-target="#nav-cvent"
type="button" role="tab" aria-controls="nav-contact" aria-selected="false">Cvent</button>
<button class="nav-link active" id="nav-cvent-tab" data-bs-toggle="tab" data-bs-target="#nav-cvent"
type="button" role="tab" aria-controls="nav-contact" aria-selected="false">Cvent</button>
<button class="nav-link active" id="nav-cvent-tab" data-bs-toggle="tab" data-bs-target="#nav-cvent"
type="button" role="tab" aria-controls="nav-contact" aria-selected="false">Cvent</button>
</div>
</nav>
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-cvent" role="tabpanel" aria-labelledby="nav-cvent-tab">
</div>
<div class="tab-pane fade show active" id="nav-cvent" role="tabpanel" aria-labelledby="nav-cvent-tab">
</div>
<div class="tab-pane fade show active" id="nav-cvent" role="tabpanel" aria-labelledby="nav-cvent-tab">
</div>
<div class="tab-pane fade show active" id="nav-cvent" role="tabpanel" aria-labelledby="nav-cvent-tab">
</div>
<div class="tab-pane fade show active" id="nav-cvent" role="tabpanel" aria-labelledby="nav-cvent-tab">
</div>
</div>
</section>
<section class="3">
...
</section>
You can achive this by having primary navbar fixed or sticky does not matter. Just set the position of the required heading to position-sticky
not sticky-top
which sticks to top which you don't want. what you want is a secondary nav bar below the primary so css
is helpfull set top
to 12%
or whatever you need just test it. This is jsFiddle link.
<section>
<h1>Section 1</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Molestias, sint fuga ipsam soluta maxime in est ipsa modi explicabo impedit natus, dolore eum, voluptas sequi dicta ullam placeat expedita quos.</p>
</section>
<section>
<h1 class="position-sticky" style="z-index: 2;top:10%">Section 2</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Molestias, sint fuga ipsam soluta maxime in est ipsa modi explicabo impedit natus, dolore eum, voluptas sequi dicta ullam placeat expedita quos.</p>
</section>
<section>
<h1>Section 3</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Molestias, sint fuga ipsam soluta maxime in est ipsa modi explicabo impedit natus, dolore eum, voluptas sequi dicta ullam placeat expedita quos.</p>
</section>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.