The Bootstrap affix is a navigation attached to the content on the same webpage. By using this plugin, we can create fixed positioned element and toggles it’s pinning on and off. It can be used to jump to a certain section in the document and it will reflect which part of the content the user is looking at. We can use affix plugin for a large amount of data and scrollbar is part of the data which displays the page.
The affix plugin toggles between following three classes:
- .affix-top
- .affix
- .affix-bottom
also read:
- The .affix-top class indicates the element in its top most position and no CSS positioning is required.
- The .affix class sets position fixed which affix the toolbar aside the element.
- The .affix-bottom class indicates the element in its bottom most position and CSS positioning fixed is used.
- The appropriate top or bottom property is required to specify position of affix element on the viewport.
The following examples demonstrate use of different techniques used for implementing the affix in Bootstrap. If you have any questions about bootstrap affix plugin, please write it in the comments section.
Bootstrap Affix via Data Attributes
We can easily create affix behavior to any element by just adding data-affix=”affix” to the element which is to be spied upon. The data-offset attributes specify how many pixels that it must scroll in order to toggle the position of an element. We use the attributes top and bottom for the pinned element to sets its position in the viewport. When the affix class is active the position is set to fixed.
The following is an example:
<!DOCTYPE html> <head> <title>Bootstrap Example</title> <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Enable Affix via Data Attributes</h2> <div class="well well-lg"> <h2>Bootstrap Affix Plugin Demo</h2> </div> <div id="my-demo" data-spy="affix" data-offset-top="60" data-offset-bottom="200"> <div class="col-md-3"> <ul class="nav nav-tabs nav-stacked affix" data-spy="affix" data-offset-top="125"> <li class="active"><a href="#myval">HTML</a></li> <li><a href="#myval1">JavaScript</a></li> <li><a href="#myval2">JQuery</a></li> </ul> </div> <div class="col-md-9"> <h2 id="myval">HTML</h2> <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. </p> <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. </p> <h2 id="myval1">JavaScript</h2> <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. </p> <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript. The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. </p> <h2 id="myval2">JQuery</h2> <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. </p> <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. </p> </div> </div> </div> </div> </body> </html>
Bootstrap Affix via JavaScript
We can enable affix plugin manually by using affix() method along with id or class selector in the javascript code as shown the below example:
<!DOCTYPE html> <head> <title>Bootstrap Example</title> <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <script> $(document).ready(function(){ $("#myval").affix({ offset:{ top:100 } }); }); </script> </head> <body> <div class="container"> <h2>Enable Affix via JavaScript</h2> <div class="well well-lg"> <h2>Bootstrap Affix Plugin Demo</h2> </div> <div class="col-md-3"> <ul class="nav nav-tabs nav-stacked affix" id="myval"> <li class="active"><a href="#myval">HTML</a></li> <li><a href="#myval1">JavaScript</a></li> <li><a href="#myval2">JQuery</a></li> </ul> </div> <div class="col-md-9"> <h2 id="myval">HTML</h2> <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. </p> <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. </p> <h2 id="myval1">JavaScript</h2> <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. </p> <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. </p> <h2 id="myval2">JQuery</h2> <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. </p> <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. </p> </div> </div> </div> </div> </body> </html>
The above example is similar to previous example, but only small difference is that, here we are using javascript code instead of data attributes. The code uses affix() method which in turn uses offset attribute to specify how many pixels that it must scroll in order to toggle the position of an element.
Bootstrap Affix Options
There are certain options which can be used with Bootstrap affix plugin as shown the below table:
Name | Type | Default Value | Description |
---|---|---|---|
offset | number | function | object |
10 | It calculate the position of scroll when it specify the number of pixels to offset. If there is a single number, then it will be applied to both top and bottom. We provide an object offset like offset: {top:100, bottom:150} . |
target | selector | node | jQuery element |
the window object | It defines target element of the affix. |
Bootstrap Affix Events
There are some event s which are used with Bootstrap’s affix class as shown in the below table:
Event | Description |
---|---|
affix.bs.affix | It triggers before the element has been affixed. |
affixed.bs.affix | It triggers after the element has been affixed. |
affix-top.bs.affix | It triggers before the element has been affixed to top. |
affixed.bs.affix | It triggers after the element has been affixed to top. |
affix-bottom.bs.affix | It triggers before the element has been affixed to bottom. |
affixed-bottom.bs.affix | It triggers after the element has been affixed to bottom. |
The following is an example:
<!DOCTYPE html> <head> <title>Bootstrap Example</title> <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <script> $(document).ready(function(){ $("#myval").affix({ offset:{ top:100 } }); $("#myval").on('affix-top.bs.affix',function(){ alert("The navigation has been affixed...doesn't scroll with page!!!"); }); }); </script> </head> <body> <div class="container"> <h2>Enable Affix via JavaScript</h2> <div class="well well-lg"> <h2>Bootstrap Affix Plugin Demo</h2> </div> <div class="col-md-3"> <ul class="nav nav-tabs nav-stacked affix" id="myval"> <li class="active"><a href="#myval">HTML</a></li> <li><a href="#myval1">JavaScript</a></li> <li><a href="#myval2">JQuery</a></li> </ul> </div> <div class="col-md-9"> <h2 id="myval">HTML</h2> <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. </p> <p>HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. HTML stands for Hyper Text Markup Language. A markup language is a set of markup tags. HTML documents contain HTML tags and plain textHTML documents are also called web pages. HTML elements form the building blocks of all websites. HTML allows images and objects to be embedded and can be used to create interactive forms. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. </p> <h2 id="myval1">JavaScript</h2> <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. </p> <p>JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. JavaScript is the programming language of the Web. JavaScript is the most popular programming language in the world. All modern HTML pages are using JavaScript.The HTML DOM (the Document Object Model) is the official W3C standard for accessing HTML elements. JavaScript can manipulate the DOM (change HTML contents).It is the language for HTML, for the Web, for computers, servers, laptops, tablets, smart phones, and more. </p> <h2 id="myval2">JQuery</h2> <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. </p> <p>jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website.jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn. jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. </p> </div> </div> </div> </div> </body> </html>
The above script uses affix-top.bs.affix event which fires before the element has been affixed to top. It means that, when we scroll the page from bottom to top, it displays the alert message when it reaches the top with specified message as shown in the code.