-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
151 lines (132 loc) · 4 KB
/
script.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
/* Data to Show*/
const data = [
{
quote: `“Happy Valentine's Day!”`,
emoji: '💗',
image: `url(assets/3.jpg)`
},
{
quote: `"You are loved!"`,
emoji: '💓',
image: `url(assets/2.jpg)`
},
{
quote: `"I hope your day is full of love!”`,
emoji: '💘',
image: `url(assets/1.jpg)`
},
{
quote: `"All you need is love.”`,
emoji: '💖',
image: `url(assets/4.jpg)`
},
{
quote: `"Share the love.”`,
emoji: '💕',
image: `url(assets/6.jpg)`
},
];
/*
Quote and Emoji
*/
// blockquote
const quote = document.querySelector("blockquote");
// emoji
const emoji = document.querySelector("p");
// background
const bgImage = document.querySelector("body");
// Check-Check: change text
quote.innerHTML = "Valentine's Day Quote Generator";
// Check-Check: change emoji text
emoji.innerHTML = "💞";
// Check-Check: background image
bgImage.style.backgroundImage = "url(/assets/5.jpg)";
/*
* Navigate to Items with Event Listeners
* Track Number of Items
*/
// initialize item number
let itemNumber = 0;
// get next button
const nextButton = document.querySelector("img.next");
// get previous button
const prevButton = document.querySelector("img.prev");
// get random button
const randButton = document.querySelector("img.random");
// listen for clicks on next button
nextButton.addEventListener("click", function () {
// update, then increase item number with each click
itemNumber = itemNumber + 1;
// if at LAST item
if (itemNumber > data.length - 1) {
// reset counting to first item
itemNumber = 0;
}
// display data based on array index
quote.innerHTML = data[itemNumber].quote;
emoji.innerHTML = data[itemNumber].emoji;
bgImage.style.backgroundImage = data[itemNumber].image;
});
// listen for clicks on previous button
prevButton.addEventListener("click", function () {
// update, then DEcrease item number with each click
itemNumber = itemNumber - 1;
// if at FIRST item
if (itemNumber < 0) {
// reset counting to last item
itemNumber = data.length - 1;
}
// display data based on array index
quote.innerHTML = data[itemNumber].quote;
emoji.innerHTML = data[itemNumber].emoji;
bgImage.style.backgroundImage = data[itemNumber].image;
});
// listen for clicks on random button
randButton.addEventListener("click", function () {
// generate random number, for total number, of items (data)
randomNumber = Math.floor(Math.random() * data.length);
// display data based random number
quote.innerHTML = data[randomNumber].quote;
emoji.innerHTML = data[randomNumber].emoji;
bgImage.style.backgroundImage = data[randomNumber].image;
});
// listen for keypress next
document.addEventListener("keyup", function () {
// Next Right Arrow
if (event.keyCode === 39) {
// update, then increase item number with each click
itemNumber = itemNumber + 1;
// if at LAST item
if (itemNumber > data.length - 1) {
// reset counting to first item
itemNumber = 0;
}
// display data based on array index
quote.innerHTML = data[itemNumber].quote;
emoji.innerHTML = data[itemNumber].emoji;
bgImage.style.backgroundImage = data[itemNumber].image;
}
// Prev Left Arrow
if (event.keyCode === 37) {
// update, then DEcrease item number with each click
itemNumber = itemNumber - 1;
// if at FIRST item
if (itemNumber < 0) {
// reset counting to last item
itemNumber = data.length - 1;
}
// display data based on array index
quote.innerHTML = data[itemNumber].quote;
emoji.innerHTML = data[itemNumber].emoji;
bgImage.style.backgroundImage = data[itemNumber].image;
}
// Random Spacebar
if (event.keyCode === 32) {
// generate random number, for total number, of items (data)
randomNumber = Math.floor(Math.random() * data.length);
// display data based random number
quote.innerHTML = data[randomNumber].quote;
emoji.innerHTML = data[randomNumber].emoji;
bgImage.style.backgroundImage = data[randomNumber].image;
}
});