Maison > Article > interface Web > bootstrap源码分析之scrollspy(滚动侦听)
Scrollspy.js
1、当滚动区域内设置的hashkey距离顶点到有效位置时,就关联设置其导航上的指定项
2、导航必须是 .nav > li > a 结构,并且a上href或data-target要绑定hashkey
3、菜单上必须有.nav样式
4、滚动区域的data-target与导航父级Id(一定是父级)要一致
<span style="color: #800000"><div id="selector" class="navbar navbar-default"> <ul class="nav navbar-nav"> <li> <a href="#one">one</a> </li> <li> <a href="#two">two</a> </li> <li> <a href="#three">three</a> </li> </ul> </div> <div data-spy="scroll" data-target="#selector" style="height:100px; overflow:hidden;overflow-y: auto;"> <h4 id="one">ibe</h4> <p>One的具体内容<br>One的具体内容<br>One的具体内容<br>One的具体内容<br>One的具体内容<br>One的具体内容<br></p> <h4 id="two">two</h4> <p>One的具体内容<br>One的具体内容<br>One的具体内容<br>One的具体内容<br>One的具体内容<br>One的具体内容<br></p> <h4 id="three">three</h4> <p>One的具体内容<br>One的具体内容<br>One的具体内容<br>One的具体内容<br>One的具体内容<br>One的具体内容<br></p> </div></span>
1、原理:当滚动容器内的hashkey位置距离容器顶部只有 offset设置的值,就会设置导航中对应的href高亮
2、如果滚动区域是body,会将滚动区域元素标记为window(构造函数中判断)
<span style="color: #0000ff">this</span>.$scrollElement = $(element).is(document.body) ? $(window) : $(element)
<span style="color: #0000ff">this</span>.$scrollElement[0].scrollHeight || Math.max(<span style="color: #0000ff">this</span>.$body[0].scrollHeight, document.documentElement.scrollHeight)
<span style="color: #0000ff">if</span> (!$.isWindow(<span style="color: #0000ff">this</span>.$scrollElement[0<span style="color: #000000">])) { offsetMethod </span>= 'position'<span style="color: #000000"> offsetBase </span>= <span style="color: #0000ff">this</span>.$scrollElement.scrollTop() <span style="color: #008000">//</span><span style="color: #008000">获取基础高度,如果滚动区域内有不参与滚动计算的内容</span> }
<span style="color: #0000ff">this</span><span style="color: #000000">.$body .find(</span><span style="color: #0000ff">this</span><span style="color: #000000">.selector) .map(</span><span style="color: #0000ff">function</span><span style="color: #000000"> () { </span><span style="color: #0000ff">var</span> $el = $(<span style="color: #0000ff">this</span><span style="color: #000000">) </span><span style="color: #0000ff">var</span> href = $el.data('target') || $el.attr('href'<span style="color: #000000">) </span><span style="color: #0000ff">var</span> $href = /^#./.test(href) && $(href) <span style="color: #008000">//</span><span style="color: #008000">获取滚动区域内的hashkey对应的元素</span> <span style="color: #0000ff">return</span><span style="color: #000000"> ($href </span>&&<span style="color: #000000"> $href.length </span>&& $href.is(':visible'<span style="color: #000000">) </span>&& [[$href[offsetMethod]().top + offsetBase, href]]) || <span style="color: #0000ff">null</span><span style="color: #000000"> }) .sort(</span><span style="color: #0000ff">function</span> (a, b) { <span style="color: #0000ff">return</span> a[0] - b[0<span style="color: #000000">] }) .each(</span><span style="color: #0000ff">function</span><span style="color: #000000"> () { that.offsets.push(</span><span style="color: #0000ff">this</span>[0<span style="color: #000000">]) that.targets.push(</span><span style="color: #0000ff">this</span>[1<span style="color: #000000">]) })</span>
<span style="color: #0000ff">var</span> scrollTop = <span style="color: #0000ff">this</span>.$scrollElement.scrollTop() + <span style="color: #0000ff">this</span>.options.offset
<span style="color: #008000">//</span><span style="color: #008000">最大可以滚动高度=滚动设置距离(offset)+ 滚动容器内容高度 - 滚动容器设置的高度</span> <span style="color: #0000ff">var</span> maxScroll = <span style="color: #0000ff">this</span>.options.offset + scrollHeight - <span style="color: #0000ff">this</span>.$scrollElement.height()
<span style="color: #0000ff">for</span> (i = offsets.length; i--;) {<span style="color: #008000">//</span><span style="color: #008000">遍历所有的offset</span> activeTarget != targets[i] <span style="color: #008000">//</span><span style="color: #008000">判断当前target是否等于activeTarget</span> && scrollTop >= offsets[i] <span style="color: #008000">//</span><span style="color: #008000">滚动高度>i元素的offset</span> && (offsets[i + 1] === undefined || scrollTop //<span style="color: #008000">i + 1元素不存在,或者i+1元素不大于滚动高度</span> && <span style="color: #0000ff">this</span>.activate(targets[i]) <span style="color: #008000">//</span><span style="color: #008000">设置i为当前活动项</span> }