-
Notifications
You must be signed in to change notification settings - Fork 0
/
lawn_slide_show.js
136 lines (124 loc) · 6.73 KB
/
lawn_slide_show.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
// This script's purpose is to create a slideshow feature on the our_work.html page that only runs
// on viewports 640px or wider that has functional next, previous, pause and resume buttons. It also
// overrides CSS to make sure that controls and the slides for the slideshow are displayed.
// Declare variable to select all of the .slides divs and place them in an array.
var slides = document.querySelectorAll('.slides');
// Declare variable to keep a counter of the .slides divs.
var slideNumber = 0;
// Set variables to detect the viewport width and to set the minimum width for the slideShow function to apply
// to viewport greater than or equal to 640px.
var viewPortWidth = document.documentElement.clientWidth || document.body.clientWidth || window.innerWidth;
var targetWidth = 640;
// Set variable for the slideinterval for the slides to advance every 3 seconds by calling slideShow every 3 seconds
var slideInterval = setInterval(slideShow, 3000);
// Set variable to determine if the slideshow is playing. By default the show plays so it will be set to true.
var showPlaying = true;
// Declare variables for the control buttons as they will be used in the nextButton, previousButton, resumeButton, and
// pauseButton on click event handlers as well as a conditional statement to set their display to inline-block if
// JS is enabled.
var pauseButton = document.querySelector('#pause');
var resumeButton = document.querySelector('#resume');
var nextButton = document.querySelector('#next');
var previousButton = document.querySelector('#previous');
// Function to run the slideshow on window load (since the script is placed at the end of the HTML).
function slideShow(n) {
// Function will only run if the user's viewPortWidth is greater than 640px.
if (viewPortWidth >= targetWidth) {
// Declare variable i as 0 for the for loop.
var i = 0;
// If is undefined (since the parameter n is defined only when the user calls the slideMove function),
// n is set to slideNumber incremented by 1 with the ++ operator so that the following conditions can be
// evaluated.
if (n == undefined) {
n = ++slideNumber;
}
// If n is greater than the index of the last element of slides (length of an array -1)
// set the slideNumber to 0 so that the first image in slides can have its display set back to block.
if (n > slides.length - 1) {
slideNumber = 0;
}
// If n is less than 0 (this would occur if the previousButton on click event occurs), set slideNumber
// to the index of the last element of slides (length of an array -1).
if (n < 0) {
slideNumber = slides.length - 1;
}
// Loop through i to set the current slide's display to none
for (i; i < slides.length; i += 1) {
slides[i].style.display = "none";
}
// Show the next slide since slideNumber is advanced by 1 when slideShow is called or the nextButton
// onclick event handler is accessed. This shows the previous slide if the previousButton on click event
// handler is accessed since parameter n is -1.
slides[slideNumber].style.display = "block";
}
};
// Function to set the control buttons display to inline-block so that they display if JS is enabled and the
// viewport width dectected by Javascript is more than or equal to 640px for tablet-sized displays and larger.
function showButtons() {
if (viewPortWidth >= targetWidth) {
pauseButton.style.display = "inline-block";
resumeButton.style.display = "inline-block";
nextButton.style.display = "inline-block";
previousButton.style.display = "inline-block";
}
};
// Call showButtons function to display the control buttons.
showButtons();
// Function to be called to manually move the slides with the parameter n to be supplied when the function is called
// in the nextButton and previousButton click event handlers.
function slideMove(n) {
// Advance the SlideNumber variable by parameter n when the slideMove function is called by passing slideNumber +=n
// as an argument to slideShow(n).
slideShow(slideNumber += n);
};
// Function to stop the slideshow.
function stop() {
// Only functions if the showPlaying variable is true.
if (showPlaying) {
// Clear the interval to stop the function slideShow.
clearInterval(slideInterval);
// Makes sure the showPlaying variable is set to false if the user clicks resumeButton resume() can
// be called by the click event handler on resumeButton.
showPlaying = false;
}
};
// On click event handler for the nextButton.
nextButton.onclick = function() {
// Pass 1 as the value for the parameter to slideMove function to advance the slide by 1.
slideMove(1);
// Call the stop() function to stop the show allow the user to manually control the slideshow without interference
// from the automatic slideshow. After the nextButton or previousButton is clicked for the first time, stop() will
// not execute since showPlaying = false, but the interval will have been cleared by the first click of the nextButton
// or previousButton while the slideshow is playing (either on page load or after a click on resumeButton).
stop();
};
// On click event handler for the previousButton.
previousButton.onclick = function() {
// Pass -1 as the value for the parameter to slideMove function to move the slide back 1 slide (advance slide -1).
slideMove(-1);
// Call the stop() function to stop the show allow the user to manually control the slideshow without interference
// from the automatic slideshow. After the nextButton or previousButton is clicked for the first time, stop() will
// not execute since showPlaying = false, but the interval will have been cleared by the first click of the nextButton
// or previousButton while the slideshow is playing (either on page load or after a click on resumeButton).
stop();
};
// On click event hander for the pauseButton to call the stop function.
pauseButton.onclick = function() {
stop();
};
// On click event hander for the pauseButton to call the resume function.
resumeButton.onclick = function() {
resume();
};
// Function to restart the slideshow.
function resume() {
// Function only executes if the showPlaying variable is false. This can only occur if the user has clicked
// the pause button. If this condition was not included, the slideShow function would be sped up.
if (!showPlaying) {
// Reset the slideInterval to 3 seconds.
slideInterval = setInterval(slideShow, 3000);
// Set the ShowPlaying variable to true so that the pause button can be used again.
showPlaying = true;
}
};
// End of of Script.