Bootstrap 的 Scrollspy(滾動監聽)可以讓頁籤式導覽列在頁面捲動時自動切換高亮狀態,很常用卻也常有人設定不出來。本文把官方文件裡五條限制逐項翻成白話,並整理成可照抄的步驟。
官方原文:Scrollspy currently requires the use of a Bootstrap nav component for proper highlighting of active links.
也就是說,被高亮的項目必須長得像 <ul class="nav">,且 <li> 底下要有 <a>。從原始碼就能看出這個硬性要求:
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() }
this.selector 被寫死成「目標容器 + .nav li > a」,所以 nav 結構是不可省略的。另外建議把 <ul class="nav"> 包在另一個容器(例如 div)裡,並給這個父容器一個 id,理由見第四點。
官方原文:Navbar links must have resolvable id targets.
白話就是 <a href="#home"> 一定要有 <div id="home"></div> 與之對應。當內容捲到該元素時,對應的 <li> 就會被加上 active。
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') }
順帶一提,HTML5 之後的頁內錨點已改用 id,舊式的 <a name="tag"> 寫法可以功成身退了。
官方原文:scrollspy requires the use of position: relative; on the element you're spying on.
如果監聽的是 body,就幫 body 加上 position:relative。如果監聽的是其他元素(這種情況其實更常見),則需要三個樣式一起上: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() }
官方原文:add data-spy="scroll" to the element you want to spy on, then add the data-target attribute with the ID or class of the parent element of any Bootstrap .nav component.
data-spy 指定「誰被監聽」,data-target 指定「要控制哪個 nav 高亮」。回頭對照第一段原始碼裡的 this.options.target,它的值就等於 data-target,所以 nav 的父容器 id 必須和這裡寫的一致。
官方原文:After adding position: relative; in your CSS, call the scrollspy via JavaScript.
$('yourTag').scrollspy({ target: 'nav-parent-div-id' })
yourTag 是承載滾動內容的元素,nav-parent-div-id 是 nav 父容器的 id。
1. 加入容器:<div id="scrollSpyID">
2. 在容器內放 nav 元件,並幫 li > a 加上 href="#tag"
3. 加入內容區:<div id="content" data-spy="scroll" data-target="#scrollSpyID">
4. 加上樣式:#content{height:500px;overflow-y:scroll;position: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="B">第二段</a><br /> <!-- 這裡要有很多內容,至少要保證可以滾動 --> <a id="C">第三段</a><br /> <!-- 這裡要有很多內容,至少要保證可以滾動 --> </div>
$(function () { $('#body').scrollspy({ target: '#sc' }); });
示例中原本三個錨點都寫成 id="A",這裡已修正為 A、B、C,否則第三、四段永遠不會被高亮。實務上最常見的失敗原因就是:忘了加 position:relative、忘記給 nav 包一層帶 id 的父容器,或是內容不夠長根本不會捲動。