diff options
author | amazingrise <8315221+AmazingRise@users.noreply.github.com> | 2020-05-24 20:22:20 +0300 |
---|---|---|
committer | amazingrise <8315221+AmazingRise@users.noreply.github.com> | 2020-05-24 20:22:20 +0300 |
commit | fe503379b2e9930459d75096f7bed0e0cfbaf334 (patch) | |
tree | 737ad073282c3a8bda63cc632f59e59bd6f7b007 /static | |
parent | b4c734f9d845231025817c5fe661b940dae81ec2 (diff) |
Finish auto collapse on contents.
Diffstat (limited to 'static')
-rw-r--r-- | static/js/toc.js | 57 |
1 files changed, 41 insertions, 16 deletions
diff --git a/static/js/toc.js b/static/js/toc.js index 075a09e..c09f598 100644 --- a/static/js/toc.js +++ b/static/js/toc.js @@ -1,22 +1,47 @@ -$().ready(function(){ +var spy = function() { var elems = $(":header"); if (elems.length == 0) { return; } - $(window).bind('scroll', function() { - var currentTop = $(window).scrollTop(); - var currentBottom = $(window).scrollTop() + $(window).height(); - var pageBottom = $('#EOF').offset().top; - - elems.each(function(idx){ - var elemTop = $(this).offset().top; - var id = $(this).attr('id'); - var navElem = $('#' + id+ '-nav'); - if(currentTop >= elemTop || currentBottom >= pageBottom){ - navElem.addClass('toc-active'); - } else { - navElem.removeClass('toc-active'); - } - }) + var currentTop = $(window).scrollTop(); + var currentBottom = $(window).scrollTop() + $(window).height(); + var pageBottom = $('#EOF').offset().top; + + var meetUnread = false + var currentIndex = -1 + elems.each(function(idx){ + var elemTop = $(this).offset().top; + var id = $(this).attr('id'); + var navElem = $('#' + id+ '-nav'); + if(currentTop + $(this).height() >= elemTop || currentBottom >= pageBottom){ + navElem.addClass('toc-active'); + } else { + if (meetUnread == false) { + meetUnread = true + currentIndex = idx - 1 + } + navElem.removeClass('toc-active'); + } + }) + if (currentIndex==-1) { + currentIndex = elems.length - 1; + } + //console.log(elems[currentIndex].id); + //Collapse them + collapseOthers("#"+elems[currentIndex].id+"-nav"); +} +var collapseOthers = function (currentId) { + //console.log(currentId); + $(currentId).parents(".collapse").each(function(idx){ + $(this).collapse("show"); }); + $(currentId).parent().next().filter(".collapse").collapse("show"); + $(".collapse").not($(currentId).parents()).not($(currentId).parent().next()).each(function(idx){ + $(this).collapse("hide"); + }); + +} +$().ready(function(){ + spy(); + $(window).bind('scroll', spy); });
\ No newline at end of file |