Bootstrap学习js插件篇之滚动监听
1、滚动监听
案例
滚动监听插件可以根据滚动条的位置自动更新所对应的导航标记。Bootstrap中文网左侧就是一个滚动监听的例子。
代码段:
- <nav id="navbar-example2" class="navbar navbar-default navbar-static" role="navigation">
- <div class="navbar-header">
- <button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-js-navbar-scrollspy">
- <span class="sr-only">Toggle-navigation</span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a href="#" class="navbar-brand">Project Name</a>
- </div>
- <div class="collapse navbar-collapse bs-js-navbar-scrollspy">
- <ul class="nav navbar-nav">
- <li><a href="#fat">@fat</a></li>
- <li><a href="#mdo">@mdo</a></li>
- <li class="dropdown">
- <a href="#" id="navbarDrop1" class="dropdown-toggle" data-toggle="dropdown">Dropdown
- <b class="caret"></b></a>
- <ul class="dropdown-menu" role="menu" aria-labelledby="navbarDrop1">
- <li><a href="#one" tabindex="-1">one</a></li>
- <li><a href="#two" tabindex="-1">two</a></li>
- <li class="divider"></li>
- <li><a href="#three" tabindex="-1">three</a></li>
- </ul>
- </li>
- </ul>
- </div>
- </nav>
- <div class="scrollspy-example" data-target="#navbar-example2" data-offset="0" data-spy="scroll">
- <span style="white-space:pre"> </span> <h4 id="fat">@fat</h4>
- <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
- <h4 id="mdo">@mdo</h4>
- <p>Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard. Freegan beard aliqua cupidatat mcsweeney's vero. Cupidatat four loko nisi, ea helvetica nulla carles. Tattooed cosby sweater food truck, mcsweeney's quis non freegan vinyl. Lo-fi wes anderson +1 sartorial. Carles non aesthetic exercitation quis gentrify. Brooklyn adipisicing craft beer vice keytar deserunt.</p>
- <h4 id="one">one</h4>
- <p>Occaecat commodo aliqua delectus. Fap craft beer deserunt skateboard ea. Lomo bicycle rights adipisicing banh mi, velit ea sunt next level locavore single-origin coffee in magna veniam. High life id vinyl, echo park consequat quis aliquip banh mi pitchfork. Vero VHS est adipisicing. Consectetur nisi DIY minim messenger bag. Cred ex in, sustainable delectus consectetur fanny pack iphone.</p>
- <h4 id="two">two</h4>
- <p>In incididunt echo park, officia deserunt mcsweeney's proident master cleanse thundercats sapiente veniam. Excepteur VHS elit, proident shoreditch +1 biodiesel laborum craft beer. Single-origin coffee wayfarers irure four loko, cupidatat terry richardson master cleanse. Assumenda you probably haven't heard of them art party fanny pack, tattooed nulla cardigan tempor ad. Proident wolf nesciunt sartorial keffiyeh eu banh mi sustainable. Elit wolf voluptate, lo-fi ea portland before they sold out four loko. Locavore enim nostrud mlkshk brooklyn nesciunt.</p>
- <h4 id="three">three</h4>
- <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
- <p>Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda. Locavore sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie gluten-free lo-fi fap aliquip. Labore elit placeat before they sold out, terry richardson proident brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats.
- </p>
- <span style="white-space:pre"> </span></div>
预览:
用法
通过data属性
通过为需要监听的页面元素(一般是<body>
)添加data-spy="scroll"
就可很轻松的为顶部导航条添加滚动监听功能。然后为其添加data-target
属性,此属性的值为任何Bootstrap中.nav
组件的父元素的ID或class。
<body data-spy="scroll" data-target=".navbar-example">
...
<div class="navbar-example">
<ul class="nav nav-tabs">
...
</ul>
</div>
...
</body>
通过JavaScript
通过JavaScript启动滚动监听:
$('body').scrollspy({ target: '.navbar-example' })
导航链接地址必须有对应的目标
导航条内的链接地址必须有对应的页面元素具有同样的ID值。例如,<a href="#home">home</a>
必须对应DOM中例如<div id="home"></div>
。
方法
.scrollspy('refresh')
使用滚动监听插件时,每当页面中从DOM中增加或删除页面元素时,都需要调用此方法以,如下:
$('[data-spy="scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh')
})
选项
可以将选项通过data属性或JavaScript传递。对于data属性,需要将选项名称放到data-
之后,例如data-offset=""
。
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
offset | number | 10 | Pixels to offset from top when calculating position of scroll. |
事件
事件类型 | 描述 |
---|---|
activate.bs.scrollspy | 当滚动监听插件将某个元素置为active时,此事件被触发。 |
$('#myScrollspy').on('activate.bs.scrollspy', function () {
// do something…
})
Bootstrap学习js插件篇之滚动监听的更多相关文章
- Bootstrap入门(二十六)JS插件3:滚动监听
很多时候我们在浏览一些网页的时候,导航条会根据我们浏览网页的进度而发生不同的变化,这种就是滚动监听. 你的顶栏导航,添加data-spy="scroll"到您想要刺探(最典型的是这 ...
- Bootstrap学习js插件篇之标签页
简单的标签页 代码: <h1 class="page-header">4.3标签页</h1> <ul class="nav nav-tabs ...
- Bootstrap学习js插件篇之提示框
案例 受到Jason Frame开发的jQuery.tipsy插件的启发,我们才把这个工具提示插件做的更好,而且此插件不依赖图片,只是使用CSS3来实现动画效果,并使用data属性存储标题. 将鼠标悬 ...
- Bootstrap学习js插件篇之下拉菜单
案例 通过此插件可以为几乎所有东西添加下拉菜单,包括导航条.标签页.胶囊式按钮. 用于导航条 导航条分为四个部分.第一部分导航头,第二部分导航列,第三部分form查询表单,第四部分导航列. <n ...
- 【Flutter学习】可滚动组件之滚动监听及控制
一,概述 ScrollController可以用来控制可滚动widget的滚动位置 二,ScrollController 构造函数 ScrollController({ double initialS ...
- 《玩转Bootstrap(JS插件篇)》笔记
导入JavaScript插件 不论是单独导入还一次性导入之前必须先导入jQuery库. 一次性导入 <script src="js/bootstrap.min.js"> ...
- 玩转Bootstrap(JS插件篇)-第1章 模态弹出框 :1-1导入JavaScript插件
导入JavaScript插件 Bootstrap除了包含丰富的Web组件之外,如前面介绍的下拉菜单.按钮组.导航.分页等.他还包括一些JavaScript的插件. Bootstrap的JavaScri ...
- bootstrap学习——javascript插件篇
飞近期做的一个小项目须要用到一个模态框和一个图片浏览插件,并把二者结合,刚好bootstrap有相应插件,下面是学习应用流程: 1. 引入js文件: 能够单个引入相应插件文件,或一次所有引入.飞 ...
- 玩转Bootstrap(JS插件篇)-第1章 模态弹出框 :1-4 模态弹出框--结构分析
模态弹出框--结构分析 Bootstrap框架中的模态弹出框,分别运用了“modal”.“modal-dialog”和“modal-content”样式,而弹出窗真正的内容都放置在“modal-con ...
随机推荐
- 这套完美的Java环境安装教程,完整,详细,清晰可观,让你一目了然,简单易懂。⊙﹏⊙
JDK下载与安装教程 2017年06月18日 22:53:16 Danishlyy1995 阅读数:349980 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- Springboot listener
在启动流程中,会出发许多ApplicationEvent.这时会调用对应的listener的onApplicationEvent方法.ApplicationEvent时观察者模式, (1) 实体继承A ...
- 【SQL】182. Duplicate Emails
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...
- leetcode 202. 快乐数 python实现
思想: 对输入数据 把每个位数平方求和 得到结果如果是1 就返回真 否则 对这个结果递归 啥时候事后返回假: 返回假 说明进入无限循环了. 啥时候会无限循环? 某一次求平方和的结果,之前得到过这个结果 ...
- redis 客户端命令
Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接 1 .CLIENT LIST 返回连接到 redis 服务的客户端列表 2 .CLIENT SETNA ...
- Failed to resolve: com.android.support:design:25.4.0
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 错误:(27, 13) Failed to resolve: com.android.s ...
- Java并发(十九):final实现原理
final在Java中是一个保留的关键字,可以声明成员变量.方法.类以及本地变量. 一旦你将引用声明作final,你将不能改变这个引用了,编译器会检查代码,如果你试图将变量再次初始化的话,编译器会报编 ...
- 每一个JavaScript开发者应该了解的浮点知识
在JavaScript开发者的开发生涯中的某些点,总会遇到奇怪的BUG——看似基础的数学问题,但却又觉得有些不对劲.总有一天,你会被告知JavaScript中的数字实际上是浮点数.试图了解浮点数和为什 ...
- 使用TensorFlow低级别的API进行编程
Tensorflow的低级API要使用张量(Tensor).图(Graph).会话(Session)等来进行编程.虽然从一定程度上来看使用低级的API非常的繁重,但是它能够帮助我们更好的理解Tenso ...
- oracle开发学习篇之集合函数
集合函数; declare type list_nested ) not null; v_all list_nested := list_nested('changan','hubei','shang ...