A Scrollspy a navigációs listában lévő linkek automatikus frissítésére szolgál a görgetés pozíciója alapján.
A következő példa bemutatja, hogyan lehet scrollspy-t létrehozni:
<!-- The scrollable area -->
<body data-bs-spy="scroll" data-bs-target=".navbar" data-bs-offset="50">
<!-- The navbar - The <a> elements are used to jump to a section in the scrollable area -->
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
...
<ul class="navbar-nav">
<li><a href="#section1">Section 1</a></li>
...
</nav>
<!-- Section 1 -->
<div id="section1">
<h1>Section 1</h1>
<p>Try to scroll this page and look at the navigation bar while scrolling!</p>
</div>
...
</body>
Próbálja ki Ön is →
<!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 href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<style>
body {
position: relative;
}
</style>
</head>
<body data-bs-spy="scroll" data-bs-target=".navbar" data-bs-offset="50">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<div class="container-fluid">
<ul class="navbar-nav">
<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>
</ul>
</div>
</nav>
<div id="section1" class="container-fluid bg-success text-white" style="padding:100px 20px;">
<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:100px 20px;">
<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 text-white" style="padding:100px 20px;">
<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>
</body>
</html>
Adja hozzá a data-bs-spy="scroll"
elemet ahhoz az elemhez, amelyet görgethető területként kell használni (gyakran ez a <body>
elem).
Ezután adja hozzá a data-bs-target
attribútumot az azonosító értékével vagy a navigációs sáv osztálynevével (.navbar
). Ez annak biztosítására szolgál, hogy a navigációs sáv csatlakozzon a görgethető területhez.
Vegye figyelembe, hogy a görgethető elemeknek meg kell egyeznie a navigációs sáv listaelemein belüli hivatkozások azonosítójával (a <div id="section1">
egyezik a <a href="#section1">
).
Az opcionális data-bs-offset
attribútum megadja a felülről eltolandó képpontok számát a görgetés helyzetének kiszámításakor. Ez akkor hasznos, ha úgy érzi, hogy a navigációs sávon belüli hivatkozások túl korán vagy túl korán megváltoztatják az aktív állapotot, amikor a görgethető elemekre ugrik. Az alapértelmezett 10 pixel.
Relatív pozicionálást igényel: A data-bs-spy="scroll" elemhez szükség van a CSS position tulajdonságára, a "relative" értékkel a megfelelő működéshez.