This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
222 lines (186 loc) · 5.79 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
(function() {
// http://www.zachstronaut.com/posts/2009/01/18/jquery-smooth-scroll-bugs.html
var scrollElement = 'html, body';
var $scrollElement;
$(function() {
$('html, body').each(function () {
var initScrollLeft = $(this).attr('scrollLeft');
$(this).attr('scrollLeft', initScrollLeft + 1);
if ($(this).attr('scrollLeft') == initScrollLeft + 1) {
scrollElement = this.nodeName.toLowerCase();
$(this).attr('scrollLeft', initScrollLeft);
return false;
}
});
$scrollElement = $(scrollElement);
});
/* Smooth scrolling of links between panels */
$(function() {
var $panels = $('.panel');
$panels.each(function() {
var $panel = $(this);
var hash = '#' + this.id;
$('a[href="' + hash + '"]').click(function(event) {
$scrollElement.stop().animate({
scrollLeft: $panel.offset().left
}, 500, 'swing', function() {
window.location.hash = hash;
});
event.preventDefault();
});
});
});
/* Panel waypoints for setting high-level location classes on body. */
$(function() {
var $body = $('body');
$('.panel')
.waypoint(function(direction) {
$body.toggleClass(this.id + '-visible', direction === 'right');
}, {
offset: '50%',
horizontal: true
})
.waypoint(function(direction) {
$body.toggleClass(this.id + '-visible', direction === 'left');
}, {
offset: '-50%',
horizontal: true
});
});
/* Force snap to panel on resize. */
$(function() {
var $window = $(window);
var timer;
$window.resize(function() {
window.clearTimeout(timer);
timer = window.setTimeout(function() {
var hash = window.location.hash ? window.location.hash : '#about';
$scrollElement.stop().animate({
scrollLeft: $(hash).offset().left
}, 200);
}, 100);
});
});
/* Fix scroll snapping during browser finds */
$(function() {
var $window = $(window);
var timer;
/* Most finds will scroll a single panel. */
var scrollToPanel = function(panel) {
$scrollElement.scrollLeft($(panel).offset().left);
};
/* Others will scroll between panels but not cause a panel scroll */
var scrollToClosestPanel = function() {
var currentScroll = $window.scrollLeft();
var panelOffsets = $.map($('.panel').get(), function(el) {
return $(el).offset().left;
});
var closestOffset = 0;
var closestDistance = 99999999;
$.each(panelOffsets, function(i, offset) {
var offsetDistance = Math.abs(currentScroll - offset);
if(offsetDistance < closestDistance) {
closestDistance = offsetDistance;
closestOffset = offset;
}
});
$scrollElement.scrollLeft(closestOffset);
};
$('.panel').scroll(function() {
window.clearTimeout(timer);
timer = window.setTimeout(scrollToPanel, 50, this);
});
/* 50ms is enough time to let the animation between panels do its
thing without triggering this debounced panel snap. */
$window.scroll(function() {
window.clearTimeout(timer);
timer = window.setTimeout(scrollToClosestPanel, 50);
});
});
/* Docs nav highlighting */
$(function() {
$('.doc-section')
.waypoint(function(direction) {
var $links = $('a[href="#' + this.id + '"]');
$links.toggleClass('active', direction === 'down');
}, {
context: '#docs',
offset: '100%'
})
.waypoint(function(direction) {
var $links = $('a[href="#' + this.id + '"]');
$links.toggleClass('active', direction === 'up');
}, {
context: '#docs',
offset: function() {
return -$(this).height();
}
});
});
/* Get Started section notification examples */
$(function() {
var notify = function(message) {
var $message = $('<p style="display:none;">' + message + '</p>');
$('.notifications').append($message);
$message.slideDown(300, function() {
window.setTimeout(function() {
$message.slideUp(300, function() {
$message.remove();
});
}, 2000);
});
};
$('#example-basic').waypoint(function() {
notify('Basic example callback triggered.');
}, { context: '.panel' });
$('#example-direction').waypoint(function(direction) {
notify('Direction example triggered scrolling ' + direction);
}, { context: '.panel' });
$('#example-offset-pixels').waypoint(function() {
notify('100 pixels from the top');
}, {
offset: 100,
context: '.panel'
});
$('#example-offset-percent').waypoint(function() {
notify('25% from the top');
}, {
offset: '25%',
context: '.panel'
});
$('#example-offset-function').waypoint(function() {
notify('Element bottom hit window top');
}, {
offset: function() {
return -$(this).height();
},
context: '.panel'
});
$('#example-context').waypoint(function() {
notify('Hit top of context');
}, { context: '.example-scroll-div' });
$('#example-handler').waypoint({
handler: function() {
notify('Handler option used');
},
offset: '50%',
context: '.panel'
});
});
/* Centering for About and Shortcut panels */
$(function() {
var $window = $(window);
var $centered = $('#about .inner, #shortcuts-examples .inner')
var center = function() {
var winHeight = $.waypoints('viewportHeight');
$centered.each(function() {
var $el = $(this);
var top = (winHeight - $el.height()) / 2;
top = top > 60 ? top : 60;
$el.css('top', top);
})
};
center();
$window.load(center).resize(center);
});
})();