Home > Web Front-end > JS Tutorial > Bootstrap must learn additional navigation (Affix) plug-in every day_javascript skills

Bootstrap must learn additional navigation (Affix) plug-in every day_javascript skills

WBOY
Release: 2016-05-16 15:04:00
Original
1734 people have browsed it

The Affix plugin allows a

to be fixed at a certain location on the page. You can also switch between using the plugin on or off. A common example is social icons. They will start at a certain position, but when a mark is clicked on the page, the
will be locked in position and will not scroll with the rest of the page.
If you want to reference the plugin's functionality individually, then you need to reference affix.js.

1. Usage

Affix plugin can be used via data attribute or via JavaScript.
1. Through the data attribute: If you need to add additional navigation (Affix) behavior to an element, you only need to add data-spy="affix" to the element that needs to be monitored. Use offsets to define when to toggle the lock and move of an element.
2. Through JavaScript: You can manually add additional navigation (Affix) to an element through JavaScript

2. Positioning through CSS

In both of the above ways of using the Affix plugin, you have to position the content via CSS. The Attached Navigation (Affix) plug-in switches between three classes, each of which presents a specific state: .affix, .affix-top and .affix-bottom. Please follow the steps below to set your own CSS for these three states (without relying on this plugin).
1. At the beginning, the plugin adds .affix-top to indicate the element is at its top position. No CSS positioning is required at this time.
2. When scrolling past an element with additional navigation (Affix) added, the actual additional navigation (Affix) should be triggered. At this time, .affix will replace .affix-top and set position: fixed; (provided by Bootstrap's CSS code).
3. If the bottom offset is defined, when the scroll reaches this position, .affix should be replaced with .affix-bottom. Since the offset is optional, setting it requires that the appropriate CSS be set as well. In this case, add position: absolute; where necessary.

3. Options

Some options are passed via data attributes or JavaScript. The options are listed in the table below:

4. Examples
Additional navigation is pasted somewhere on the screen to implement the anchor function.
1. Basic examples

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

 <div class="container">
 <div class="jumbotron" style="height:150px">
  <h1>Bootstrap Affix</h1>
 </div>
 <div class="row">
  <div class="col-xs-3" id="myScrollspy">
  <ul class="nav nav-pills nav-stacked" data-spy="affix"data-offset-top="150">
   <li class="active">
   <a href="#section-1">第一部分 </a>
   </li>
   <li>
   <a href="#section-2">第二部分</a>
   </li>
   <li>
   <a href="#section-3">第三部分</a>
   </li>
   <li>
   <a href="#section-4">第四部分</a>
   </li>
   <li>
   <a href="#section-4">第五部分</a>
   </li>
  </ul>
  </div>
  <div class="col-xs-9">
  <h2 id="section-1">第一部分</h2>
  <p>
   ...
  </p>
  <h2 id="section-2">第二部分</h2>
  <p>
   ...
  </p>
  <h2 id="section-3">第三部分</h2>
  <p>
   ...
  </p>
  <h2 id="section-4">第四部分</h2>
  <p>
   ...
  </p>
  <h2 id="section-5">第四部分</h2>
  <p>
   ...
  </p>
  </div>

 </div>
 </div>

Copy after login

2. CSS part of navigation

ul.nav-pills {
 width: 200px;
}
ul.nav-pills.affix {
 top: 30px;
}
//JavaScript 代替 data-spy="affix" data-offset-top="125"

$('#myAffix').affix({
 offset : {
 top : 150
 }
})

Copy after login

We use top by default, but of course it can also be bottom by default. This positioning method is positioned directly through CSS.

//设置成 bottom

ul.nav-tabs.affix-bottom {
 bottom: 30px;
}
//设置成 bottom

$('#myAffix').affix({
 offset : {
 bottom : 150
 }
})

Copy after login

Affix contains several events, as follows:

//其他雷同

$('#myAffix').on('affixed-top.bs.affix', function() {
 alert('触发!');
});
Copy after login

For more information, please refer to: Bootstrap Learning Tutorial

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template