Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

BootstrapJS Scrollspy


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 ourBootstrap Scrollspy Tutorial.

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


Via data-* Attributes

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

Then add thedata-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> matches<a href="#section1">).

The optionaldata-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 CSSposition 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>
    <li><a href="#section1">Section 1</a></li>
    ...
</nav>

<!-- Section 1 -->
<div>
  <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="".

NameTypeDefaultDescriptionTry it
offsetnumber10 Specifies the number of pixels to offset from top when calculating the position of scrollTry it

Scrollspy Methods

The following table lists all available scrollspy methods.

MethodDescriptionTry it
.scrollspy("refresh")When adding and removing elements from the scrollspy, this method can be used to refresh the documentTry it

Scrollspy Events

The following table lists all available scrollspy events.

EventDescriptionTry it
activate.bs.scrollspyOccurs when a new item becomes activated by the scrollspyTry 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 theAffix 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 »



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

-->
[8]ページ先頭

©2009-2026 Movatter.jp