/
sidebar.js
129 lines (93 loc) · 3.71 KB
/
sidebar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
$(function() {
var JSLSidebar = function() {
if ($(window).width() < 700) {
$("#content").removeClass("sidebar-present");
} else {
$("#content").addClass("sidebar-present");
}
$("#content").css('height',$("article").height());
function addToggler() {
$('.toggle-sidebar').bind('click', function(e) {
e.preventDefault();
if ($('body').hasClass('collapse-sidebar')) {
$("#content").css('margin-right','240px');
$('body').removeClass('collapse-sidebar');
} else {
$("#content").css('margin-right','20px');
$('body').addClass('collapse-sidebar');
}
});
var sections = $('aside.sidebar > section');
if (sections.length > 1) {
sections.each(function(section, index){
if ((sections.length >= 3) && index % 3 === 0) {
$(section).addClass("first");
}
var count = ((index +1) % 2) ? "odd" : "even";
$(section).addClass(count);
});
}
if (sections.length >= 3){ $('aside.sidebar').addClass('thirds'); }
}
function positionContentWithinWindow(topSection,topPosition) {
var y = $(window).scrollTop();
if (y >= topPosition) {
topSection.addClass('fixed');
if (topSection.height() > $(window).height()) {
var contentDifference = $(window).height() - topSection.height() + (y - topPosition);
if (contentDifference > 40) { contentDifference = 20; }
topSection.css('bottom',contentDifference);
} else {
topSection.css('bottom','');
topSection.css('top','0');
}
} else {
topSection.removeClass('fixed');
}
}
function highlightTheContentItemWhenInTheNeighorhood() {
var distanceFromTop = $(window).scrollTop() + $(window).height() / 2;
var itemIndex = 0;
$("#content article h2").each(function(index,element) {
var item = $(element)
if (distanceFromTop > item.offset().top) {
itemIndex = index;
}
});
$("aside section").each(function(index,element) {
$(element).css("background","");
});
$($("aside section")[itemIndex + 1]).css("background-color","rgba(0,0,0,0.1)");
}
function enableContentToFollowWithWindowScrollAndResize() {
var topSection = $("aside.sidebar div:first");
var topPosition = topSection.offset().top - parseFloat(topSection.css('margin-top').replace(/auto/, 0));
$(window).scroll(function(event) {
positionContentWithinWindow(topSection,topPosition);
highlightTheContentItemWhenInTheNeighorhood();
});
$(window).resize(function(event) {
positionContentWithinWindow(topSection,topPosition);
});
}
function populateWithContent() {
generateLinksForContent("aside.sidebar div:first","article header");
generateLinksForContent("aside.sidebar div:first","#content article h2");
}
function generateLinksForContent(targetSelector,contentSelector) {
var contentItemTarget = $(targetSelector);
$(contentSelector).each(function(index,headerItem) {
var contentItem = $(headerItem);
var contentItemSlug = contentItem.text().trim().toLowerCase().replace(/\s/g,'-');
var anchorName = "<a name='" + contentItemSlug + "' />";
$(anchorName).insertBefore(contentItem);
var contentAnchorLink = "<section><h3><a href='#" + contentItemSlug + "'>" + contentItem.text().trim() + "</h3></a></section>";
contentItemTarget.append(contentAnchorLink);
});
}
addToggler();
enableContentToFollowWithWindowScrollAndResize();
populateWithContent();
}
var sidebar = new JSLSidebar();
})