-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathslide.js
224 lines (198 loc) · 5.64 KB
/
slide.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
219
220
221
222
223
224
/*
Copyright © 2013 Greg Smith
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall
be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
*/
$(function() {
var $slides = $(".slide");
var i;
var totalSlides;
var slideMode = false;
var slide;
var totalSlides = $slides.length;
var moving = false;
var marginBottomHeight;
// Add IDs for all slides
for (i = 0; i < $slides.length; i++) {
if (!$slides.eq(i).attr("id")) {
$slides.eq(i).attr("id", "slide" + (i + 1));
}
}
// Scroll to hash location in case ID didn't exist on page load
var hash = window.location.hash;
window.location.hash = "";
window.location.hash = hash;
function enterSlideMode() {
slideMode = true;
var $slide = findClosestSlide();
marginBottomHeight = $("body").css("margin-bottom");
$("body").css("margin-bottom",
($(window).height() - $(".slide").last().height()) + "px");
gotoSlide($slide);
message("Slide Mode");
$("#start").hide();
$("#done, #next, #prev").show();
}
function exitSlideMode() {
slideMode = false;
$(".slide").removeClass("hidden", "fast");
$("body").css("margin-bottom", marginBottomHeight);
message("Webpage Mode");
$("#start").show();
$("#done, #next, #prev").hide();
}
function changeHash(hash, andScroll) {
if (andScroll) {
window.location.hash = hash;
}
else {
var $elem = $("#" + hash);
var id = $elem.attr("id");
$elem.attr("id", "temp-" + Math.random());
window.location.hash = hash;
$elem.attr("id", id);
}
}
function gotoSlide(n, andScroll) {
moving = true;
if (n < 0 || n >= totalSlides) {
return;
}
var $slide;
if (typeof n !== "number") {
$slide = n;
n = $slide.index();
}
else {
$slide = $slides.eq(n);
}
slide = n;
$slides.not($slide).addClass("hidden");
var id;
changeHash($slide.attr("id"), andScroll);
// The turd is my workaround for
// https://github.com/scottjehl/Device-Bugs/issues/1
$("#turd").remove();
$slide.removeClass("hidden", "fast", function() {
moving = false;
if (isTouchDevice()) {
$("<div>", {
id: "turd",
style: "height: 1px;"
}).appendTo("body");
}
});
}
function findClosestSlide() {
var $closest;
var scrollTop = $(window).scrollTop();
$slides.each(function() {
var $slide = $(this);
if (!$closest || $slide.position().top <
scrollTop + ($(window).height() / 2)) {
$closest = $slide;
}
});
return $closest;
}
function message(str) {
var $msg = $("<div></div>", {
"class": "message",
text: str
});
$("body").prepend($msg);
window.setTimeout(function() {
$msg.addClass("hidden", "fast", "", function() {
$msg.remove();
});
}, 200);
}
$(document).on("keydown", debounce(function(e) {
// escape
if (e.keyCode === 27) {
if (!slideMode) {
enterSlideMode();
}
else {
exitSlideMode();
}
}
// left
else if (e.keyCode === 39) {
if (slideMode) {
gotoSlide(slide + 1, true);
}
}
// right
else if (e.keyCode === 37) {
if (slideMode) {
gotoSlide(slide - 1, true);
}
}
}, 200))
.on("scroll", debounce(function(e) {
var $closestSlide = findClosestSlide();
if (moving) {
return;
}
if (!slideMode) {
changeHash($closestSlide.attr("id"), false);
}
else if ($closestSlide.attr("id") !==
$slides.eq(slide).attr("id")) {
gotoSlide($closestSlide);
}
}));
function debounce(func, threshold, execAsap) {
var timeout;
return function debounced () {
var obj = this, args = arguments;
function delayed () {
if (!execAsap)
func.apply(obj, args);
timeout = null;
};
if (timeout)
clearTimeout(timeout);
else if (execAsap)
func.apply(obj, args);
timeout = setTimeout(delayed, threshold || 100);
};
}
function isTouchDevice() {
return !!('ontouchstart' in window) // works on most browsers
|| !!('onmsgesturechange' in window); // works on ie10
};
if (isTouchDevice()) {
$("body")
.prepend($("#touch-template").text())
.css("margin-bottom", $("#touch").outerHeight(true) + "px");
$("#start").on("click", debounce(function(e) {
enterSlideMode();
}));
$("#done").on("click", debounce(function(e) {
exitSlideMode();
}));
$("#next").on("click", debounce(function(e) {
gotoSlide(slide + 1, true);
}));
$("#prev").on("click", debounce(function(e) {
gotoSlide(slide - 1, true);
}));
}
});