50 lines
1.4 KiB
HTML
50 lines
1.4 KiB
HTML
<script type="text/javascript" charset="utf-8">
|
|
$(function() {
|
|
var headerHeight = $("#header").height();
|
|
var offsets = [];
|
|
var current = -1;
|
|
|
|
function endpoint(scrollDistance) {
|
|
if (scrollDistance < offsets[0]) {
|
|
return -1;
|
|
} else {
|
|
for (var id = offsets.length; id > 0; id--) {
|
|
if (scrollDistance > offsets[id - 1]) {
|
|
return id - 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$("h2").each(function(i) {
|
|
offsets.push($(this).offset().top - headerHeight)
|
|
});
|
|
|
|
$("#content").append('<h2 class="fixed" style="display: none"><span> </span></h2>');
|
|
var fixed_h2 = $("h2.fixed");
|
|
var fixed_span = $("h2.fixed span");
|
|
|
|
$("#content").scroll(function() {
|
|
var scrollDistance = $("#content").attr('scrollTop');
|
|
var now = endpoint(scrollDistance);
|
|
|
|
if (now !== current) {
|
|
$("#sidebar li").removeClass("current");
|
|
current = now;
|
|
if (current < 0) {
|
|
fixed_h2.hide();
|
|
} else if (current >= 0) {
|
|
var heading = $($("h2 span")[current]).text();
|
|
$("#sidebar a[href|=#" + heading.replace(' ', '-') + "]").parent().addClass("current");
|
|
fixed_span.text(heading);
|
|
fixed_h2.show();
|
|
}
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|