Bootstrap每天必學(xué)之滾動(dòng)監(jiān)聽
本文為大家介紹Bootstrap滾動(dòng)監(jiān)聽,供大家學(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)聽的話,導(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)單的說,就是<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)做過Web開發(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)聽Body的滾動(dòng),那么你必須給body添加position:relative樣式
--- 如果監(jiān)聽的不是Body,而是其他得元素[貌似這種方式常見],那么你需要添加三個(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)聽的元素,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)聽組件scrollspy.js使用方法詳解
- bootstrap監(jiān)聽滾動(dòng)實(shí)現(xiàn)頭部跟隨滾動(dòng)
- BootStrap下拉菜單和滾動(dòng)監(jiān)聽插件實(shí)現(xiàn)代碼
- 學(xué)習(xí)Bootstrap滾動(dòng)監(jiān)聽 附調(diào)用方法
- 全面解析Bootstrap中scrollspy(滾動(dòng)監(jiān)聽)的使用方法
- Bootstrap教程JS插件滾動(dòng)監(jiān)聽學(xué)習(xí)筆記分享
- Bootstrap滾動(dòng)監(jiān)聽(Scrollspy)插件詳解
- Bootstrap+Vue滑動(dòng)監(jiān)聽Scrollspy實(shí)現(xiàn)餐廳餐品展示
相關(guān)文章
JavaScript+html5 canvas制作色彩斑斕的正方形效果
這篇文章主要介紹了JavaScript+html5 canvas制作色彩斑斕的正方形效果,實(shí)例分析了JavaScript結(jié)合html5 canvas實(shí)現(xiàn)圖形動(dòng)態(tài)繪制的技巧,需要的朋友可以參考下2016-01-01原生js三級(jí)聯(lián)動(dòng)的簡(jiǎn)單實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇原生js三級(jí)聯(lián)動(dòng)的簡(jiǎn)單實(shí)現(xiàn)代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-06-06javascript 實(shí)現(xiàn) 秒殺,團(tuán)購(gòu) 倒計(jì)時(shí)展示的記錄 分享
這篇文章介紹了javascript 實(shí)現(xiàn) 秒殺,團(tuán)購(gòu) 倒計(jì)時(shí)展示的記錄方法,有需要的朋友可以參考一下2013-07-07JavaScript+html實(shí)現(xiàn)前端頁(yè)面隨機(jī)二維碼驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了JavaScript+html實(shí)現(xiàn)前端頁(yè)面隨機(jī)二維碼驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06JS實(shí)現(xiàn)間歇滾動(dòng)的運(yùn)動(dòng)效果實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)間歇滾動(dòng)的運(yùn)動(dòng)效果,涉及javascript定時(shí)器觸發(fā)動(dòng)態(tài)改變頁(yè)面元素的相關(guān)操作技巧,需要的朋友可以參考下2016-12-12javascript iframe內(nèi)的函數(shù)調(diào)用實(shí)現(xiàn)方法
用下面的方法可以調(diào)用iframe中的函數(shù),實(shí)現(xiàn)一些比較特殊的效果,不過能跨域的。2009-07-07Javascript 定時(shí)器調(diào)用傳遞參數(shù)的方法
Javascript 定時(shí)器調(diào)用傳遞參數(shù)的方法,需要的朋友可以參考下。2009-11-11微信小程序?qū)崿F(xiàn)多選刪除列表數(shù)據(jù)功能示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)多選刪除列表數(shù)據(jù)功能,涉及微信小程序列表數(shù)據(jù)讀取、顯示、刪除等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01Javascript和jquery在selenium的使用過程
這篇文章主要介紹了Javascript和jquery在selenium的使用過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10