当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Bootstrap Scrollspy源码学习

Bootstrap Scrollspy源码学习

2019年03月23日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了bootstrap scrollspy插件的具体代码,供大家参考,具体内容如下

导航栏scrollspy例子

<!-- 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" rel="external nofollow" rel="external nofollow" >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>

垂直scrollspy例子

<body data-spy="scroll" data-target="#myscrollspy" data-offset="20">

 <div class="container">
 <div class="row">
  <nav class="col-sm-3" id="myscrollspy">
  <ul class="nav nav-pills nav-stacked">
   <li><a href="#section1" rel="external nofollow" rel="external nofollow" >section 1</a></li>
   ...
  </ul>
  </nav>
  <div class="col-sm-9">
  <div id="section1"> 
   <h1>section 1</h1>
   <p>try to scroll this page and look at the navigation list while scrolling!</p>
  </div> 
  ...
  </div>
 </div>
 </div>

</body>


scrollspy的使用

使用scrollspy只需在对应的html元素里添加几个关键的属性:
- data-spy=”scroll”
添加到需要滚动的元素中,比如最常见的body元素,或者container。
- data-target=”selector”
添加到需要滚动的元素中,selector指示的是控制滚动的元素比如”.navbar”, “#myscrollspy”。
- <a href=”#section”>section</a>
在控制滚动的元素中用link链接到对应的位置。注意链接的id要跟对应位置元素的id相匹配。例如,<div id=”section1”>与<a href=”#seciton1”。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网