|
| 1 | +// Adapted from Javascript Garden, a MIT project. |
| 2 | +function Sections(page) { |
| 3 | + this.page = page; |
| 4 | + this.init(); |
| 5 | +} |
| 6 | + |
| 7 | +Sections.prototype = { |
| 8 | + init: function(attribute) { |
| 9 | + this.heights = this.page.nav.find('ul').map(function(idx, ele) { |
| 10 | + return $(this).outerHeight(); |
| 11 | + }).get(); |
| 12 | + }, |
| 13 | + |
| 14 | + map: function() { |
| 15 | + this.names = $('h2').map(function(idx, ele) { |
| 16 | + return { |
| 17 | + id: this.id, |
| 18 | + offset: $(this).offset().top + 30, |
| 19 | + title: $(this).find(':header:first').html() |
| 20 | + }; |
| 21 | + }).get(); |
| 22 | + }, |
| 23 | + |
| 24 | + highlight: function() { |
| 25 | + |
| 26 | + var scroll = this.page.window.scrollTop(), |
| 27 | + articleID = this.names[this.names.length - 1].id; |
| 28 | + |
| 29 | + $('a').removeClass('active'); |
| 30 | + |
| 31 | + var $el; |
| 32 | + |
| 33 | + for(var i = 0, l = this.names.length; i < l; i++) { |
| 34 | + if (this.names[i].offset > scroll) { |
| 35 | + $el = $("[href='#" + this.names[i].id + "']"); |
| 36 | + var s = $el.parents('ul')[0]; |
| 37 | + |
| 38 | + // $el.addClass('active'); |
| 39 | + |
| 40 | + if (s !== window.section) { |
| 41 | + //$(window.section).slideUp(); |
| 42 | + $(window.section).hide(); |
| 43 | + //$(s).slideDown(); |
| 44 | + $(s).show(); |
| 45 | + window.section = s; |
| 46 | + } |
| 47 | + |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | + }, |
| 52 | + |
| 53 | + updateLinks: function(index) { |
| 54 | + if (index !== this.names.length - 1) { |
| 55 | + this.setLink(this.links.next, this.names[index + 1]); |
| 56 | + } else { |
| 57 | + //this.links.next.slideUp(100); |
| 58 | + this.links.next.hide(); |
| 59 | + } |
| 60 | + |
| 61 | + if (index !== 0) { |
| 62 | + this.setLink(this.links.prev, this.names[index - 1]); |
| 63 | + } else { |
| 64 | + //this.links.prev.slideUp(100); |
| 65 | + this.links.next.hide(); |
| 66 | + } |
| 67 | + }, |
| 68 | + |
| 69 | + setLink: function(ele, data) { |
| 70 | + ele.slideDown(100).attr('href', '#' + data.id) |
| 71 | + .find('.nav_section_name').html(data.title); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +function Page() { |
| 76 | + $.extend(true, this, { |
| 77 | + window: $(window), |
| 78 | + nav: $('.nav > ul > li'), |
| 79 | + section: null, |
| 80 | + articule: null |
| 81 | + }); |
| 82 | + |
| 83 | + this.sections = new Sections(this); |
| 84 | + this.init(); |
| 85 | +} |
| 86 | + |
| 87 | +Page.prototype = { |
| 88 | + init: function() { |
| 89 | + var that = this, |
| 90 | + mainNav = $('.toc'); |
| 91 | + |
| 92 | + $.extend(this, { |
| 93 | + scrollLast: 0, |
| 94 | + resizeTimeout: null |
| 95 | + }); |
| 96 | + |
| 97 | + this.window.scroll(function() { |
| 98 | + that.onScroll(); |
| 99 | + }); |
| 100 | + |
| 101 | + this.window.resize(function() { |
| 102 | + that.onResize(); |
| 103 | + }); |
| 104 | + |
| 105 | + that.sections.map(); |
| 106 | + setTimeout(function() { |
| 107 | + that.sections.highlight(); |
| 108 | + }, 10); |
| 109 | + }, |
| 110 | + |
| 111 | + onScroll: function() { |
| 112 | + if ((+new Date()) - this.scrollLast > 50) { |
| 113 | + this.scrollLast = +new Date(); |
| 114 | + this.sections.highlight(); |
| 115 | + } |
| 116 | + }, |
| 117 | + |
| 118 | + onResize: function() { |
| 119 | + clearTimeout(this.resizeTimeout); |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +$(document).ready(function() { |
| 124 | + if ($(window).width() > 481) { |
| 125 | + var page = new Page(); |
| 126 | + page.scrolllast = new Date(); |
| 127 | + } |
| 128 | + |
| 129 | + //$('.side ul ul').hide(); |
| 130 | + //$('.side ul ul').first().show(); |
| 131 | +}); |
0 commit comments