Bootstrap Affix Plugin

Bootstrap Affix Plugin

The Bootstrap Affix plugin allows you to fix an element to the top or bottom of the page when the user scrolls past a certain point. It is useful when you have a navigation menu or other important element that you want to keep visible at all times. Here’s an example of how to use the Affix plugin:

				
					<!-- Navigation menu -->
<nav id="navbar-example3" class="navbar navbar-light bg-light" data-spy="affix" data-offset-top="50">
  <a class="navbar-brand" href="#">Affix Example</a>
  <ul class="nav nav-pills">
    <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>
</nav>

<!-- Main content -->
<div class="container" style="margin-top: 100px;">
  <h4 id="section1">Section 1</h4>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

  <h4 id="section2">Section 2</h4>
  <p>Nullam sit amet est sed tellus rhoncus molestie.</p>

  <h4 id="section3">Section 3</h4>
  <p>Vestibulum sit amet libero in quam lacinia blandit ac vitae massa.</p>
</div> 
				
			

Bootstrap Affix Plugin

In this example, we have a navigation menu with links to three sections of the web page. The data-spy=”affix” attribute on the nav element activates the Affix plugin. The data-offset-top=”50″ attribute sets the offset value (in pixels) at which the navigation should become fixed. In this case, the navigation will become fixed 50 pixels from the top of the page.

To activate the Affix plugin, you need to initialize it with JavaScript:

				
					$(function () {
  $('#navbar-example3').affix({
    offset: {
      top: 50
    }
  })
})
				
			

This code initializes the Affix plugin on the navigation menu and sets the offset value to 50 pixels from the top of the page.

You can customize the appearance and behavior of the Affix plugin by adjusting the classes and attributes. For example, you can change the position of the fixed element (top or bottom of the page), add animation effects, or use JavaScript to control when the element becomes fixed or unfixed.

Join To Get Our Newsletter
Spread the love