Bootstrap每天必學(xué)之滾動(dòng)監(jiān)聽(tīng)
本文為大家介紹Bootstrap滾動(dòng)監(jiān)聽(tīng),供大家學(xué)習(xí),具體內(nèi)容如下
1. Scrollspy currently requires the use of a Bootstrap nav component for proper highlighting of active links.
---- 使用滾動(dòng)監(jiān)聽(tīng)的話,導(dǎo)航欄必須采用 class="nav"的nav組件才可以:
下面是源代碼中的一段,標(biāo)紅的部分可以證明這一點(diǎn):
使用ScrollSpy的時(shí)候,需要采用<ul class="nav">標(biāo)簽,并且在<li>下必須有<a>標(biāo)簽。
注:另外我們需要把<ul class="nav">標(biāo)簽放到另一個(gè)容器內(nèi)(如div),并給父容器添加一個(gè)id屬性(這一點(diǎn)在第4節(jié)有介紹)
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 <a href="#home">home</a> must correspond to something in the DOM like <div id="home"></div>.
--- 簡(jiǎn)單的說(shuō),就是<li>下的<a>標(biāo)簽必須有一個(gè)href="#id"屬性,并且在滾動(dòng)的內(nèi)容里面,必須有對(duì)應(yīng)的<a id="id"></a>這樣的標(biāo)簽;當(dāng)內(nèi)容滾動(dòng)到<a id="id">標(biāo)簽時(shí),對(duì)應(yīng)的<li>的<a href="#id">就會(huì)自動(dòng)被選中。
--其實(shí)這一點(diǎn)做過(guò)Web開(kāi)發(fā)的朋友都知道,在之前的HTML版本中,錨標(biāo)記 通常采用<a name="tag">這樣的方式,但HTML5中的錨標(biāo)記已經(jīng)拋棄了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 <body>. When scrollspying on elements other than the <body>, be sure to have a height set and overflow-y: scroll; applied.
--- 如果監(jiān)聽(tīng)Body的滾動(dòng),那么你必須給body添加position:relative樣式
--- 如果監(jiān)聽(tīng)的不是Body,而是其他得元素[貌似這種方式常見(jiàn)],那么你需要添加三個(gè)樣式: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 <body>). Then add the data-target attribute with the ID or class of the parent element of any Bootstrap .navcomponent.
--- 你需要給滾動(dòng)內(nèi)容的標(biāo)簽添加 data-spy="scroll"屬性和data-target屬性
data-spy 屬性指明了被監(jiān)聽(tīng)的元素,data-target屬性指明滾動(dòng)時(shí)需要控制的nav高亮顯示
再看一次下面的初始化源代碼,標(biāo)紅的位置,this.options.target的值,就等于滾動(dòng)內(nèi)容元素的data-target的值,看到這里,你或許已經(jīng)想到,在定義.nav組件的時(shí)候,我們需要把.nav放在另一個(gè)容器內(nèi)(比如div),且該容器需要有一個(gè)id屬性(與這里data-target需要設(shè)置的值相同)。
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 就是要承載滾動(dòng)內(nèi)容的元素的ID,nav-parent-div-id 就是.nav元素的父元素的id(也就是data-target的值)
亂七八糟寫了一堆,下面總結(jié)一個(gè)簡(jiǎn)單的幾個(gè)步驟:
1. 添加標(biāo)簽<div id="scrollSpyID">
2. 在標(biāo)簽內(nèi)添加.nav組件,并給li->a添加href="#tag"屬性
3. 添加<div id="content" data-spy="scroll" data-target="#scrollSpyID">;
4. 添加樣式#content{height:500px;overflow-y:scroll;opsition:relative;}
5. 添加腳本$('#content').scrollspy({target:'scrollSpyID'});
最后來(lái)個(gè)小栗子:
<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 /> <!-- 這里要有很多內(nèi)容,至少要保證可以滾動(dòng) --> <a id="A">第二段</a><br /> <!-- 這里要有很多內(nèi)容,至少要保證可以滾動(dòng) --> <a id="A">第三段</a><br /> <!-- 這里要有很多內(nèi)容,至少要保證可以滾動(dòng) --> </div>
$(function () {
$('#body').scrollspy({ target: '#sc' });
});
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附兩個(gè)精彩的專題:Bootstrap學(xué)習(xí)教程 Bootstrap實(shí)戰(zhàn)教程
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Bootstrap滾動(dòng)監(jiān)聽(tīng)組件scrollspy.js使用方法詳解
- bootstrap監(jiān)聽(tīng)滾動(dòng)實(shí)現(xiàn)頭部跟隨滾動(dòng)
- BootStrap下拉菜單和滾動(dòng)監(jiān)聽(tīng)插件實(shí)現(xiàn)代碼
- 學(xué)習(xí)Bootstrap滾動(dòng)監(jiān)聽(tīng) 附調(diào)用方法
- 全面解析Bootstrap中scrollspy(滾動(dòng)監(jiān)聽(tīng))的使用方法
- Bootstrap教程JS插件滾動(dòng)監(jiān)聽(tīng)學(xué)習(xí)筆記分享
- Bootstrap滾動(dòng)監(jiān)聽(tīng)(Scrollspy)插件詳解
- Bootstrap+Vue滑動(dòng)監(jiān)聽(tīng)Scrollspy實(shí)現(xiàn)餐廳餐品展示
相關(guān)文章
JavaScript 動(dòng)態(tài)創(chuàng)建VML的方法
JavaScript 動(dòng)態(tài)創(chuàng)建VML的方法,需要的朋友可以參考下。2009-10-10
JavaScript設(shè)計(jì)模式之裝飾者模式介紹
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之裝飾者模式介紹,通一個(gè)類來(lái)動(dòng)態(tài)的對(duì)另一個(gè)類的功能對(duì)象進(jìn)行前或后的修飾,給它輔加一些額外的功能; 這是對(duì)一個(gè)類對(duì)象功能的裝飾,裝飾的類跟被裝飾的類,要求擁有相同的訪問(wèn)接口方法(功能),需要的朋友可以參考下2014-12-12
輕量級(jí)JS Cookie插件js-cookie的使用方法
js-cookie插件是一個(gè)JS操作cookie的插件,源文件只有3.34 KB,非常輕量級(jí),js-cookie也支持npm和Bower安裝和管理,下面看看js-cookie的具體用法2018-03-03
深入理解JavaScript系列(16) 閉包(Closures)
本章我們將介紹在JavaScript里大家經(jīng)常來(lái)討論的話題 —— 閉包(closure)。閉包其實(shí)大家都已經(jīng)談爛了。盡管如此,這里還是要試著從理論角度來(lái)討論下閉包,看看ECMAScript中的閉包內(nèi)部究竟是如何工作的2012-04-04
ES6知識(shí)點(diǎn)整理之函數(shù)數(shù)組參數(shù)的默認(rèn)值及其解構(gòu)應(yīng)用示例
這篇文章主要介紹了ES6知識(shí)點(diǎn)整理之函數(shù)數(shù)組參數(shù)的默認(rèn)值及其解構(gòu)應(yīng)用,結(jié)合實(shí)例形式分析了ES6函數(shù)數(shù)組參數(shù)解構(gòu)賦值和默認(rèn)值的設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
es7學(xué)習(xí)教程之Decorators(修飾器)詳解
這篇文章主要給大家介紹了關(guān)于es7中Decorators(修飾器)的相關(guān)資料,文中通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
JavaScript下申明對(duì)象的幾種方法小結(jié)
在JavaScript中可以用下面的幾種方法申明對(duì)象:(從"Truly"的文章中學(xué)到)2008-10-10
詳解搭建es6+devServer簡(jiǎn)單開(kāi)發(fā)環(huán)境
這篇文章主要介紹了詳解搭建es6+devServer簡(jiǎn)單開(kāi)發(fā)環(huán)境,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
JS實(shí)現(xiàn)延遲隱藏功能的方法(類似QQ頭像鼠標(biāo)放上展示信息)
下面小編就為大家分享一篇JS實(shí)現(xiàn)延遲隱藏功能的方法(類似QQ頭像鼠標(biāo)放上展示信息),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12

