为什么80%的码农都做不了架构师?>>>   

总体来说,用起来不是特别满意,先放一放吧。

导航条示例

滚动监听插件会根据滚动的位置,自动更新导航条的目标。滚动在导航条下面的区域,查看active类的改变。弹出菜单的子项也同样会被高亮。

<nav id="navbar-example2" class="navbar navbar-default" role="navigation"><h3 class="navbar-brand">Project Name</h3><ul class="nav nav-pills"><li class="nav-item"><a class="nav-link" href="#fat">@fat</a></li><li class="nav-item"><a class="nav-link" href="#mdo">@mdo</a></li><li class="nav-item dropdown"><a class="nav-link dropdown-toggle active" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a><div class="dropdown-menu"><a class="dropdown-item" href="#one">one</a><a class="dropdown-item" href="#two">two</a><div role="separator" class="dropdown-divider"></div><a class="dropdown-item active" href="#three">three</a></div></li></ul>
</nav><div data-spy="scroll" data-target="#navbar-example2" data-offset="0" style="position:relative;height: 15rem;overflow:auto;"><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>
</div>

注意:下一行的sytle属性。

<div data-spy="scroll" data-target="#navbar-example2" data-offset="0" style="position:relative;height: 15rem;overflow:auto;">

PS:如图所示滚动的时候确实会动。但存在一个bug。

是当目标的#id=fat这行,到达页面<nav>时,才会触发效果。所以如果手动点@mdo,则导航栏active还在@fat元素上。

但当整个页面存在滚动条时,#id=fat才能在可视范围的最上方。

于是我想到换一种方法。

但依然无用。或许,换一种方法替代。

@mdo 的上一行建立一个空的标签,#id换成@mdo的方法。这样是不是就可以了呢?

上图是滚动时的样子。感觉上还ok。但是当点击后,就差强人意了。

下图是点击后的效果。

用法

需要Bootstrap导航条

滚动监听现在需要用到Bootstrap nav 组件 以适当地高亮激活的链接。

需要相对位置

无论如何应用这个方法,滚动监听需要在你要监听的元素上用position:relative;。在多数情况下,这个需要监听的元素就是<body>。当在除了<body>之外的元素上进行滚动监听时,请确保应用了一个height设置以及overflow-y:scroll;

利用data属性

要想方便地在你的顶部导航条上添加滚动监听,请在你想监听的元素上添加data-spy="scroll"(最典型的就是<body>)。然后添加data-target属性,属性值是任何Bootstrap.nav组件的父元素的ID

body {position: relative;
}
<body data-spy="scroll" data-target="#navbar-example">...<div id="navbar-example"><ul class="nav nav-tabs" role="tablist">...</ul></div>...
</body>

利用JavaScript

在你的CSS中添加了position: relative;之后,利用JavaScript调用滚动监听:

$('body').scrollspy({ target: '#navbar-example' })

导航条链接必须拥有可解析的ID目标。

不可见的目标元素会被忽略。

方法

.scrollspy(‘refresh’)

在使用滚动监听的同时,如果在DOM中添加或移除了元素,你需要调用刷新方法,如下所示:

$('[data-spy="scroll"]').each(function () {var $spy = $(this).scrollspy('refresh')
})

选项

可以利用data属性或者JavaScript传递选项。如果用data属性,请把选项名追加到data-后面,比如说写成data-offset=""

名称 类型 默认值 description
offset number 10 从顶部滚下来多少像素开始计算位置

事件

事件类型 描述
activate.bs.scrollspy 当一个新项被滚动监听激活时,会触发该事件。
$('#myScrollspy').on('activate.bs.scrollspy', function () {// do something…
})

转载于:https://my.oschina.net/asktao/blog/647372

4.18、Bootstrap V4自学之路-----内容---滚动监听相关推荐

  1. 4.22、Bootstrap V4自学之路-----内容---轮播

    为什么80%的码农都做不了架构师?>>>    示例 <div id="carousel-example-generic" class="caro ...

  2. 4.19、Bootstrap V4自学之路-----内容---提示冒泡

    为什么80%的码农都做不了架构师?>>>    我一直没有调试出来,包裹引入tether.min.js仍然无法出发效果. mark着,回头再看.先熟悉文档. 概述 在使用提示冒泡插件 ...

  3. 4.4、Bootstrap V4自学之路------组件---表单

    为什么80%的码农都做不了架构师?>>>    表单控件 下面是一个Bootstrap支持的表控件以及自定义类的完整列表.额外的文档对每个组都是可用的. 类 作用 支持的变量 .fo ...

  4. 1.3、Bootstrap V4自学之路------起步---浏览器支持

    为什么80%的码农都做不了架构师?>>>    支持的浏览器 Bootstrap 支持所有的主流浏览器和平台的最新的.稳定的版本. 在 Windows 中, **我们支持 Inter ...

  5. 4.7、Bootstrap V4自学之路------组件---广告屏

    为什么80%的码农都做不了架构师?>>>    示例 单独的一个空的标签 <div class="jumbotron"><!-- 背景色是灰色的 ...

  6. bootstrap中轮播图、模态框、提示框/弹出框、滚动监听、弹性布局、响应式flex、多媒体对象

    轮播图: bootstrap封装了轮播图的功能,其具体如下: 类名 描述 .carousel 创建一个轮播图块的容器,实质是做布局用:且此容器应该有一个di属性,其属性值提供给下面左右按钮href锚点 ...

  7. Bootstrap 滚动监听

    滚动监听 Bootstrap滚动监听插件Scrollspy能够根据滚动的位置,自动更新导航条中相应的导航项. 滚动监听插件需要 bootstrap-scrollspy.js 文件支持,在使用该插件之前 ...

  8. Bootstrap学习(七)modal模态框、data属性、过渡事件、滚动监听事件

    模态框 模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. <div class=" ...

  9. Bootstrap(滚动监听)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

最新文章

  1. Cissp-【第1章 安全和风险管理】-2021-1-7(99页-162页)
  2. 本地开发好的 SAP Fiori Elements 应用,如何部署到 ABAP 服务器上?
  3. button的图文设置
  4. 我希望早几年知道的5个Unix命令
  5. VS2010快捷键大全----养成良好的习惯
  6. linux 脚本 if then,shell学习之if-then语法
  7. 在Linux安装QQ,只需几步
  8. oracle solaris 10 系统 下载,更新 Oracle Solaris 11 系统中的软件
  9. 文字识别 SDK 11 给大家看一个手册
  10. Transformer 综合讲解15-20章
  11. win10系统如何打开.swf视频文件,flash palyer无法使用
  12. 抖音SEO优化源码,企业号搜索排名系统,矩阵分发。
  13. htc e9pw android 6.0,HTC One E9+(E9pw 联通4G)一键ROOT教程,简单ROOT
  14. seo网站优化人员的工作是什么样的?
  15. php class 构造_PHP 类与构造函数解析
  16. 未被定义的 “智能座舱”,如何将产业化进行到底
  17. zed相机拆机_使用TX2+ZED相机运行ORBSLAM2和LearnVIORB
  18. python高德 查询县_Python获取高德地图省市区县列表
  19. 大众继续深耕SUV市场:5款新车型先于上海车展登场,ID. ROOMZZ成点睛之笔...
  20. 商汤科技2023春季校园招聘正式启动!

热门文章

  1. java设计模式之设计原则②依赖倒置原则
  2. Writing your first Django app--Django 第一步
  3. linux子系统led,Linux设备驱动GPIO子系统
  4. (原創) 如何对array或struct做初始化? (memset()) (C/C++) (C)
  5. 它是最神秘的黑客组织:来自战斗民族 专黑美国
  6. Lucene系列:(9)搜索结果排序
  7. 英特尔Nehalem微架构三级缓存原理学习
  8. WPF简单的口算案例
  9. Strtus2工作流程及原理
  10. SQL2K数据库开发二十二之索引操作在视图上创建索引