本文为大家介绍Bootstrap滚动监听,供大家学习,具体内容如下
1. Scrollspy currently requires the use of a Bootstrap nav component for proper highlighting of active links.
---- 使用滚动监听的话,导航栏必须采用 class="nav"的nav组件才可以:
下面是源代码中的一段,标红的部分可以证明这一点:
使用ScrollSpy的时候,需要采用73a72cdc17fc2b29bb35d64b4687fa7c标签,并且在25edfb22a4f469ecb59f1190150159c6下必须有3499910bf9dac5ae3c52d5ede7383485标签。
注:另外我们需要把73a72cdc17fc2b29bb35d64b4687fa7c标签放到另一个容器内(如div),并给父容器添加一个id属性(这一点在第4节有介绍)
function ScrollSpy(element, options) { this.$body = $(document.body) this.$scrollElement = $(element).is(document.body) ? $(window) : $(element) this.options = $.extend({}, ScrollSpy.DEFAULTS, options) this.selector = (this.options.target || '') + ' .nav li > a' this.offsets = [] this.targets = [] this.activeTarget = null this.scrollHeight = 0 this.$scrollElement.on('scroll.bs.scrollspy', $.proxy(this.process, this)) this.refresh() this.process() }
2. Navbar links must have resolvable id targets. For example, a bdbe26872a334f3b7c83b8ae7c50ac47home5db79b134e9f6b82c0b36e0489ee08ed must correspond to something in the DOM like 5f3b34213db5dea3192e3beea655c52f16b28748ea4df4d9c2150843fecfba68.
--- 简单的说,就是25edfb22a4f469ecb59f1190150159c6下的3499910bf9dac5ae3c52d5ede7383485标签必须有一个href="#id"属性,并且在滚动的内容里面,必须有对应的5ffad543fdd9d71340ebb3c2c670dbf35db79b134e9f6b82c0b36e0489ee08ed这样的标签;当内容滚动到5ffad543fdd9d71340ebb3c2c670dbf3标签时,对应的25edfb22a4f469ecb59f1190150159c6的2dac5b3c1a71acb02bf03dd588b09a33就会自动被选中。
--其实这一点做过Web开发的朋友都知道,在之前的HTML版本中,锚标记 通常采用bc8f08fb0993388c993dc7bf88c0ef43这样的方式,但HTML5中的锚标记已经抛弃了name属性,而是采用id属性
ScrollSpy.prototype.activate = function (target) { this.activeTarget = target this.clear() var selector = this.selector + '[data-target="' + target + '"],' + this.selector + '[href="' + target + '"]' var active = $(selector) .parents('li') .addClass('active') if (active.parent('.dropdown-menu').length) { active = active .closest('li.dropdown') .addClass('active') } active.trigger('activate.bs.scrollspy') }
3. No matter the implementation method, scrollspy requires the use of position: relative; on the element you're spying on. In most cases this is the 6c04bd5ca3fcae76e30b72ad730ca86d. When scrollspying on elements other than the 6c04bd5ca3fcae76e30b72ad730ca86d, be sure to have a height set and overflow-y: scroll; applied.
--- 如果监听Body的滚动,那么你必须给body添加position:relative样式
--- 如果监听的不是Body,而是其他得元素[貌似这种方式常见],那么你需要添加三个样式:position:relative;height:500px;overflow-y:scroll;
ScrollSpy.prototype.refresh = function () { var that = this var offsetMethod = 'offset' var offsetBase = 0 this.offsets = [] this.targets = [] this.scrollHeight = this.getScrollHeight() if (!$.isWindow(this.$scrollElement[0])) { offsetMethod = 'position' offsetBase = this.$scrollElement.scrollTop() }
4. To easily add scrollspy behavior to your topbar navigation, add data-spy="scroll" to the element you want to spy on (most typically this would be the 6c04bd5ca3fcae76e30b72ad730ca86d). Then add the data-target attribute with the ID or class of the parent element of any Bootstrap .navcomponent.
--- 你需要给滚动内容的标签添加 data-spy="scroll"属性和data-target属性
data-spy 属性指明了被监听的元素,data-target属性指明滚动时需要控制的nav高亮显示
再看一次下面的初始化源代码,标红的位置,this.options.target的值,就等于滚动内容元素的data-target的值,看到这里,你或许已经想到,在定义.nav组件的时候,我们需要把.nav放在另一个容器内(比如div),且该容器需要有一个id属性(与这里data-target需要设置的值相同)。
function ScrollSpy(element, options) { this.$body = $(document.body) this.$scrollElement = $(element).is(document.body) ? $(window) : $(element) this.options = $.extend({}, ScrollSpy.DEFAULTS, options) this.selector = (this.options.target || '') + ' .nav li > a' this.offsets = [] this.targets = [] this.activeTarget = null this.scrollHeight = 0 this.$scrollElement.on('scroll.bs.scrollspy', $.proxy(this.process, this)) this.refresh() this.process() }
5. After adding position: relative; in your CSS, call the scrollspy via JavaScript:
$('yourTag').scrollspy({ target: 'nav-parent-div-id' })
-- yourTag 就是要承载滚动内容的元素的ID,nav-parent-div-id 就是.nav元素的父元素的id(也就是data-target的值)
乱七八糟写了一堆,下面总结一个简单的几个步骤:
1. 添加标签240da3ef6b880ee42429071353b2bdbe
2. 在标签内添加.nav组件,并给li->a添加href="#tag"属性
3. 添加d111d6fe33c5d1eadcb1b41531cb3333;
4. 添加样式#content{height:500px;overflow-y:scroll;opsition:relative;}
5. 添加脚本$('#content').scrollspy({target:'scrollSpyID'});
最后来个小栗子:
<style type="text/css"> #body { position: relative; height: 500px; overflow-y: scroll; } </style>
<div id="sc"> <ul class="nav nav-pills"> <li class="active"> <a href="#A">第一段</a> </li> <li> <a href="#B">第二段</a> </li> <li> <a href="#C">第三段</a> </li> </ul> </div>
<div id="body" class="container-fluid" data-spy="scroll" data-target="#sc"> <a id="A">第一段</a><br /> <!-- 这里要有很多内容,至少要保证可以滚动 --> <a id="A">第二段</a><br /> <!-- 这里要有很多内容,至少要保证可以滚动 --> <a id="A">第三段</a><br /> <!-- 这里要有很多内容,至少要保证可以滚动 --> </div>
$(function () { $('#body').scrollspy({ target: '#sc' }); });
以上就是本文的全部内容,希望对大家的学习有所帮助。