W3Schools.com (2024)

JS Scrollspy (scrollspy.js)

The Scrollspy plugin is used to automatically update links in a navigation list based on scroll position.

For a tutorial about Scrollspy, read our Bootstrap Scrollspy Tutorial.

Tip: The Scrollspy plugin is often used together with theAffix plugin.

Via data-* Attributes

Add data-spy="scroll" to the element that should be used as the scrollable area (often this is the <body> element).

Then add the data-target attribute with a value of the id or the class name of the navigation bar (.navbar). This is to make sure that the navbar is connected with the scrollable area.

Note that scrollable elements must match the ID of the links inside the navbar's list items (<div id="section1"> matches <a href="#section1">).

The optional data-offset attribute specifies the number of pixels to offset from top when calculating the position of scroll. This is useful when you feel that the links inside the navbar changes the active state too soon or too early when jumping to the scrollable elements. Default is 10 pixels.

Requires relative positioning: The element with data-spy="scroll" requires the CSS position property, with a value of "relative" to work properly.

Example

<!-- The scrollable area -->
<body data-spy="scroll" data-target=".navbar" data-offset="50">

<!-- The navbar - The <a> elements are used to jump to a section in the scrollable area -->
<nav class="navbar navbar-inverse navbar-fixed-top">
...
<ul class="nav 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>

Try it Yourself »

Via JavaScript

Enable manually with:

Example

$('body').scrollspy({target: ".navbar"})

Try it Yourself »

Scrollspy Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-offset="".

Name Type Default Description Try it
offset number 10 Specifies the number of pixels to offset from top when calculating the position of scroll Try it

Scrollspy Methods

The following table lists all available scrollspy methods.

Method Description Try it
.scrollspy("refresh") When adding and removing elements from the scrollspy, this method can be used to refresh the document Try it

Scrollspy Events

The following table lists all available scrollspy events.

Event Description Try it
activate.bs.scrollspy Occurs when a new item becomes activated by the scrollspy Try it

More Examples

Scrollspy with animated scroll

How to add a smooth page scroll to an anchor on the same page:

Smooth scrolling

// Add scrollspy to <body>
$('body').scrollspy({target: ".navbar", offset: 50});

// Add smooth scrolling on all links inside the navbar
$("#myNavbar a").on('click', function(event) {

// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {

// Prevent default anchor click behavior
event.preventDefault();

// Store hash
var hash = this.hash;

// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){

// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});

} // End if

});

Try it Yourself »

Scrollspy & Affix

Using the Affix plugin together with the Scrollspy plugin:

Horizontal Menu (Navbar)

<body data-spy="scroll" data-target=".navbar" data-offset="50">

<nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197">
...
</nav>

</body>

Try it Yourself »

Vertical Menu (Sidenav)

<body data-spy="scroll" data-target="#myScrollspy" data-offset="15">

<nav class="col-sm-3" id="myScrollspy">
<ul class="nav nav-pills nav-stacked" data-spy="affix" data-offset-top="205">
...
</nav>

</body>

Try it Yourself »


W3schools Pathfinder

Track your progress - it's free!

W3Schools.com (2024)
Top Articles
Is Nike, Inc. (NKE) stock halal to invest in?
What is uploading? | Definition from TechTarget
Moon Stone Pokemon Heart Gold
Trevor Goodwin Obituary St Cloud
فیلم رهگیر دوبله فارسی بدون سانسور نماشا
How Many Cc's Is A 96 Cubic Inch Engine
Koordinaten w43/b14 mit Umrechner in alle Koordinatensysteme
Polyhaven Hdri
Tlc Africa Deaths 2021
Magic Mike's Last Dance Showtimes Near Marcus Cedar Creek Cinema
MADRID BALANZA, MªJ., y VIZCAÍNO SÁNCHEZ, J., 2008, "Collares de época bizantina procedentes de la necrópolis oriental de Carthago Spartaria", Verdolay, nº10, p.173-196.
PGA of America leaving Palm Beach Gardens for Frisco, Texas
Ucf Event Calendar
Max 80 Orl
Planets Visible Tonight Virginia
Craigslist Labor Gigs Albuquerque
Buying risk?
R/Afkarena
Mary Kay Lipstick Conversion Chart PDF Form - FormsPal
Baywatch 2017 123Movies
Troy Bilt Mower Carburetor Diagram
Der Megatrend Urbanisierung
Exterior insulation details for a laminated timber gothic arch cabin - GreenBuildingAdvisor
Our History
Geometry Review Quiz 5 Answer Key
Culver's Flavor Of The Day Taylor Dr
Dtlr Duke St
MyCase Pricing | Start Your 10-Day Free Trial Today
Jermiyah Pryear
Gilchrist Verband - Lumedis - Ihre Schulterspezialisten
Bay Area Craigslist Cars For Sale By Owner
Speechwire Login
4.231 Rounded To The Nearest Hundred
Wbli Playlist
Wow Quest Encroaching Heat
Where Do They Sell Menudo Near Me
Seymour Johnson AFB | MilitaryINSTALLATIONS
New York Rangers Hfboards
Tal 3L Zeus Replacement Lid
Myfxbook Historical Data
My Locker Ausd
Tyler Perry Marriage Counselor Play 123Movies
Homeloanserv Account Login
Paul Shelesh
Crystal Glassware Ebay
Searsport Maine Tide Chart
VerTRIO Comfort MHR 1800 - 3 Standen Elektrische Kachel - Hoog Capaciteit Carbon... | bol
Human Resources / Payroll Information
Michaelangelo's Monkey Junction
The Significance Of The Haitian Revolution Was That It Weegy
Cbs Scores Mlb
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 6211

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.