Skip to content

Commit 1934efe

Browse files
committed
Highlight nav on scroll
1 parent 7201b5f commit 1934efe

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ <h4>Returns:</h4>
233233
</body>
234234

235235
<script src="api-viewer/bower_components/jquery/dist/jquery.js"></script>
236+
<script src="api-viewer/bower_components/underscore/underscore.js"></script>
236237
<script src="api-viewer/bower_components/combyne/dist/combyne.js"></script>
237238
<script src="scripts/prism.js"></script>
238239
<script src="scripts/api.js"></script>

scripts/api.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,42 @@
5252
});
5353
});
5454

55+
// Size the navigation bar correctly.
5556
scrollable.height($("html").innerHeight() - 230);
5657

5758
$(window).on("resize", function() {
5859
scrollable.height($("html").innerHeight() - 230);
5960
});
6061

62+
var windowHeight = $(window).height();
63+
64+
// Store the url to match against.
65+
var elms = $(".about, .inner-content, .inner-block li, h4[id]").each(function() {
66+
$(this).data({
67+
url: "#" + (this.id || ""),
68+
top: $(this).offset().top
69+
});
70+
});
71+
72+
var children = listApi.find("a");
73+
74+
$(document).on("scroll ready", _.throttle(function() {
75+
var doc = $(this);
76+
77+
elms.each(function() {
78+
var pos = doc.scrollTop();
79+
var el = $(this);
80+
81+
if (el.data("top") > pos && el.data("top") < pos + (windowHeight / 2)) {
82+
children.removeClass("active");
83+
84+
var active = children.filter("[href='" + el.data("url") + "']");
85+
86+
active.addClass("active").parents(elms).addClass("active");
87+
}
88+
});
89+
}, 50));
90+
6191
// Highlight the new code blocks.
6292
Prism.highlightAll();
6393

styles/api.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ ul.api-contents table h3 {
139139
padding-left: 30px;
140140
list-style: none;
141141
border-radius: 4px;
142+
padding-right: 30px;
142143
}
143144

144145
.api-content .inner-block strong.cName,

0 commit comments

Comments
 (0)