-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.js
408 lines (338 loc) · 11.7 KB
/
slider.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
jQuery( document ).ready( function( $ ) {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* GLOBAL VARIABLES *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
var NUM_SLIDES = $( '.ss2-slide-list > li' ).length;
var SLIDE_MAX = NUM_SLIDES - 1;
var MAX_TITLE_LENGTH = 50;
//console.log ("SLIDE_MAX -> " + SLIDE_MAX);
var allSlideTitles = $( '.ss2-slide-list > li .ss2-title-and-date' ); // keeping this out here so others can use it
var allSlideTitlesClone = allSlideTitles.clone(); // a copy for the large size
var currentSlide = 0; // begins at 0
var slideTime = 10000; // time ea slide displays in ms
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* THE CODE *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* Based on the slides, populate the title container div with titles
*/
function populateTitles() {
$( '.ss2-container-titles' ).append(allSlideTitlesClone); // move allSlideTitles to target div
}
/**
* Truncate titles which are too long. Only applies to side-col titles
* for large view.
*/
function truncateTitles() {
let titleStrings = $( '.ss2-container-titles .ss2-title a' );
titleStrings.each(function(i,e){
console.log(e.textContent);
let title = e.textContent;
if ( title.length > MAX_TITLE_LENGTH ) {
title = title.substring(0,MAX_TITLE_LENGTH);
e.textContent = title + " . . ."; // add ellipsis
}
});
}
/**
*
* Sets up the heights of the title elements, based on the height of the
* slide image container
*
*/
function setTitleHeights() {
var slideContainerHeight = $( '.ss2-slide-list' ).height() + $( '.ss2-slide-nav' ).height();
var borderHeight = NUM_SLIDES; // compensate for total height taken by borders (should be v small)
var indivTitleHeight = (slideContainerHeight - borderHeight) / NUM_SLIDES;
// assign height to title element class
// use innerheight because it takes padding into consideration
$( '.ss2-container-titles .ss2-title-and-date' ).innerHeight(indivTitleHeight);
// re-visibilize if coming back up from small screen layout
allSlideTitlesClone.show();
}
/**
*
* Prints # of dots based on # of slides
*
*/
function generateDots() {
$( '.ss2-nav-dots' ).append( "<ul>" );
for (var count = 0; count < NUM_SLIDES; count++) {
$( '.ss2-nav-dots ul' ).append( "<li><i class='fa fa-circle-o'></i></>" ); // UNICODE "White Circle" U+25CB
}
}
/**
*
* Manipulates the navigation dots; highlights current slide corresponding
* dot and removes previous dot highlight.
* Used by any function which moves the slides in any direction.
* Should not need to manipulate currentSlide global. Purely reactionary.
* @param {current} The current slide, given by calling method
* @param {previous} The previous slide, given by calling method
*
*/
function manipulateDots(current,previous) {
// get dots
var dots = $( '.ss2-nav-dots ul li' );
// use global to turn empty dot into filled dot
dots.eq(current).html( "<i class='fa fa-circle'></i>" );
// check if previous needs to wrap (if it's bigger than max)
// this is to adjust for when clicking previous arrow/going backward
if ( previous > SLIDE_MAX ) {
previous = 0;
}
// turn previously filled dot to empty dot
dots.eq(previous).html( "<i class='fa fa-circle-o'></i>" );
//console.log("dots: current- " + current + ", prev- " + previous);
}
/*
* Determines whether to hide all the titles or to calculate their height.
* Based on window width.
* Note: this means that title switching action will not work for the small layout
* on large devices & the breakpoint switchoff must be perfect.
* @param {number} window_width The current width of the window in pixels
*/
function titleSetup(window_width) {
allSlideTitles.hide() // this group should always be hidden -- only show current one (below)
if (window_width <= 991) { // Bootstrap 4 md screens are >= 992
// small & xsmall devices
// hide all side titles
allSlideTitlesClone.hide()
// show current on-image one
allSlideTitles.eq(currentSlide).show();
} else {
// medium + up devices
truncateTitles();
setTitleHeights();
}
}
/**
*
* Sets up the slides to begin sliding motion -- hides all slides but the
* first, highlights current title card, and sets up dots.
*
*/
function slideSetup() {
// ---------------------------------
// SETUP - initial page load
// hide all slides but the first one
// ---------------------------------
var allSlides = $( '.ss2-slide-list > li' ); // get all slides in easy array format
allSlides.hide(); // hide all slides
// show the first (is first because currentSlide is initialized to 0)
allSlides.eq(currentSlide).show();
allSlideTitlesClone.eq(currentSlide).addClass( 'current' ); // highlight the first title
manipulateDots(currentSlide); // setup initial dot to be filled
}
/**
*
* Advance slides by 1
* modifies currentSlide global
*
*/
function slideForward() {
// get all slides
var allSlides = $( '.ss2-slide-list > li' );
// add 1 to currentSlide global
// make sure it is not max, or wrap around to 0
if ( currentSlide < SLIDE_MAX ) {
currentSlide++;
} else {
currentSlide = 0;
}
// hide all slides
allSlides.hide();
// show slide.eq(currentSlide)
allSlides.eq(currentSlide).show();
// hide prev slide title, e.g. slideTitles.eq(currentSlide-1)
allSlideTitlesClone.eq(currentSlide-1).removeClass( 'current' );
// show current slide title
allSlideTitlesClone.eq(currentSlide).addClass( 'current' );
// advance dot
manipulateDots(currentSlide,currentSlide-1);
// make sure titles are sized correctly to the image
// this accounts for variable height images
// setTitleHeights();
titleSetup($(window).width());
}
/**
*
* Retreat slides by 1
* modifies currentSlide global
* note: seems to make a weird skipping effect when changing slides,
* e.g. from the first to the last when going around, or sometimes
* in the middle. need to check if this repeats on other machines.
* may not be an issue, since this method will be used once at a time
* when arrows are clicked, and not for continuous motion like
* slideForward()
*
*/
function slideBackward() {
//console.log("----")
// get all slides
var allSlides = $( '.ss2-slide-list > li' );
// subtract 1 from currentSlide global
// make sure it is not 0, or wrap around to max
if ( currentSlide > 0 ) {
currentSlide--;
} else {
currentSlide = SLIDE_MAX;
}
// hide all slides
allSlides.hide();
// show slide.eq(currentSlide)
allSlides.eq(currentSlide).show();
// hide prev slide title, slideTitle.eq(currentSlide+1)
if (currentSlide == SLIDE_MAX) {
allSlideTitlesClone.eq(0).removeClass( 'current' );
//console.log("remove from " + 0);
} else {
allSlideTitlesClone.eq(currentSlide+1).removeClass( 'current' );
//console.log("remove from " + (currentSlide+1));
}
// show current slide title
allSlideTitlesClone.eq(currentSlide).addClass( 'current' );
// change dot
manipulateDots(currentSlide,currentSlide+1);
// make sure titles are sized correctly to the image
// this accounts for variable height images
// setTitleHeights();
titleSetup($(window).width());
}
/**
*
* Jump to any slide by given index
* modifies currentSlide global
* @param {newSlide} the slide to which the slider will jump when this
* method is called
*
*/
function slideJumpTo( newSlide ) {
// test if current is within bounds
if ( newSlide >= 0 && newSlide <= SLIDE_MAX) {
var allSlides = $( '.ss2-slide-list > li' );
// hide all slides
allSlides.hide();
// show new slide as per parameter
allSlides.eq(newSlide).show();
// first remove title class 'current' so no fancy math is needed later
allSlideTitlesClone.eq(currentSlide).removeClass('current');
// update title highlight
allSlideTitlesClone.eq(newSlide).addClass('current');
// update dot
//console.log("jump: current- " + newSlide + ", prev- " + currentSlide);
manipulateDots(newSlide,currentSlide); //next,prev as parameters
currentSlide = newSlide; // finally, update currentSlide
// make sure titles are sized correctly to the image
// this accounts for variable height images
// setTitleHeights();
titleSetup($(window).width());
} // else do nothing
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* CLICK EVENTS *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
*
* LEFT ARROW CONTROL
* Goes back a single slide.
*
*/
$( '.ss2-arrow-left' ).click(function(){
// stop the sliding action
clearInterval(slideAction);
// go backward 1
slideBackward();
// restart the sliding action
slideAction = setInterval(slideForward,slideTime);
});
/**
*
* RIGHT ARROW CONTROL
* Goes forward a single slide.
*
*/
$( '.ss2-arrow-right' ).click(function(){
// stop the sliding action
clearInterval(slideAction);
// go forward 1
slideForward();
// restart sliding action
slideAction = setInterval(slideForward,slideTime);
});
/**
*
* PAUSE
* Click event used for testing. Works on element with class 'pause'.
* Elements needs to be added into the slider HTML manually to be used.
*
*/
$( '.pause' ).click(function(){
if ( $('.pause').text() == "pause" ) {
clearInterval(slideAction);
$('.pause').text("unpause");
} else {
slideAction = setInterval(slideForward,slideTime);
$('.pause').text("pause");
}
});
/**
*
* DOTS . . .
* For when a dot is clicked! Will go to the slide corresponding to that dot.
* This will modify @currentSlide because of the call to slideJumpTo().
*
* Note: Had to use on() instead of click() because dots ul was generated
* by jQuery; click() only attaches to elements which existed at
* document ready.
*
*/
$( '.ss2-nav-dots' ).on("click", "ul li", function(event) {
// get dots ul li
var dots = $( '.ss2-nav-dots ul li ');
// stall sliding action
clearInterval(slideAction);
// check what # of dot was clicked
// previously had assigned value to currentSlide, but that seemed
// to mess up the movement of the dots... once used a separate var,
// nextSlide, everything cleared up.
nextSlide = dots.index(event.currentTarget); // for some reason event.target stopped working
slideJumpTo(nextSlide);
// restart sliding action - should take care of the dot change too
slideAction = setInterval(slideForward,slideTime);
});
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Running the code *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
*
* SETUP
* Setup elements & initiate the sliding action.
*
*/
generateDots();
populateTitles();
slideSetup(); // this comes before setTitleHeights() for correct calculations
titleSetup($( window ).width());
manipulateDots();
var slideAction = setInterval(slideForward, slideTime);
/**
*
* WINDOW RESIZE
* Every time window resizes, check the slides height and assign it to
* the height of the titles.
*
*/
$( window ).resize( function() {
// setTitleHeights();
titleSetup($(window).width());
});
});