-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Expand file tree
/
Copy pathscript.js
More file actions
285 lines (184 loc) · 7.13 KB
/
script.js
File metadata and controls
285 lines (184 loc) · 7.13 KB
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
'use strict';
///////////////////////////////////////
// Modal window
const modal = document.querySelector('.modal');
const overlay = document.querySelector('.overlay');
const btnCloseModal = document.querySelector('.btn--close-modal');
const btnsOpenModal = document.querySelectorAll('.btn--show-modal');
const btnScrollTo = document.querySelector('.btn--scroll-to');
const section1 = document.querySelector('#section--1');
/////////////////////////////////////////////////////////
const openModal = function (e) {
e.preventDefault();
modal.classList.remove('hidden');
overlay.classList.remove('hidden');
};
const closeModal = function () {
modal.classList.add('hidden');
overlay.classList.add('hidden');
};
btnsOpenModal.forEach( (val) => val.addEventListener('click', openModal))
btnCloseModal.addEventListener('click', closeModal);
overlay.addEventListener('click', closeModal);
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && !modal.classList.contains('hidden')) {
closeModal();
}
});
btnScrollTo.addEventListener('click', function(e) {
const s1coords = section1.getBoundingClientRect();
console.log(s1coords);
console.log(e.target.getBoundingClientRect());
console.log('Current Scroll (x/y) ', window.pageXOffset, window.pageYOffset);
console.log('height/width viewport', document.documentElement.clientHeight,
document.documentElement.clientWidth);
// old way
// window.scrollTo({
// left: s1coords.left + window.pageYOffset,
// top: s1coords.top + window.pageYOffset,
// behavior: 'smooth',
// });
// new way for smooth scroll
section1.scrollIntoView({behavior: 'smooth'});
});
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
//page Navigation
// not optimal adding event listener for all elements with .nav__link class
// document.querySelectorAll('.nav__link').forEach( function(el) {
// el.addEventListener('click', function(e) {
// e.preventDefault();
// const id = this.getAttribute('href');
// console.log(id);
// document.querySelector(id).scrollIntoView({behavior: 'smooth'});
// })
// });
/*
1. Add event listner to common parent elemnt
2. Determine wht element originated the event
*/
document.querySelector('.nav__links').addEventListener('click', function(e) {
e.preventDefault();
console.log(e.target);
// matching strategy
if(e.target.classList.contains('nav__link')) {
const id = e.target.getAttribute('href');
console.log(id);
document.querySelector(id).scrollIntoView({behavior: 'smooth'});
}
})
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
console.log(document.documentElement);
console.log(document.head);
console.log(document.body);
const header = document.querySelector('.header');
const allSelections = document.querySelectorAll('.section');
console.log(allSelections);
document.getElementById('section--1');
const allButtons = document.getElementsByTagName('button');
console.log(allButtons);
document.getElementsByClassName('btn');
console.log(document.getElementsByClassName('btn'));
//creating and Inserting elements
// insertAdjacentHTML
const message = document.createElement('div');
message.classList.add('cookie-message');
message.textContent = "We use cookied for imporved functionality and analytics.";
message.innerHTML =
'We use cookied for imporved functionality and analytics. <button class = "btn btn--close-cookie">Got it!</button>' ;
// header.prepend(message);
header.append(message);
// header.append(message.cloneNode(true));
// Delete elements
document.querySelector('.btn--close-cookie').addEventListener('click', function() {
// message.remove();
message.parentElement.removeChild(message);
});
// Styles
// message.style.backgroundColor = "#37383d";
message.style.width = "120%";
// how to get styles
console.log(getComputedStyle(message).height);
message.style.height = Number.parseFloat(getComputedStyle(message).height,10) + 30 + 'px';
//working with custom Css variables
// document.documentElement.style.setProperty('--color-primary', 'orangered');
//Atributes
const logo = document.querySelector('.nav__logo');
// only works if attributes are standard, can not be custom
console.log(logo.alt);
console.log(logo.className);
// setting atributies
logo.alt = 'Beautiful Minimalist logo'
console.log(logo.alt);
console.log(logo.src); // prints absolute path
console.log(logo.getAttribute('src')); // gets realtive path
//Data Attributes
console.log(logo.dataset.versionNumber);
//Classes
// logo.classList.add();
// logo.classList.remove();
// logo.classList.toggle();
// logo.classList.contains();
//Implementing Smooth Scroll
console.log(btnScrollTo);
console.log(section1);
// const h1 = document.querySelector('h1');
// const alertH1 = function(e) {
// alert('addEventListener: Great! ');
// // Method 1 of remove event listener
// // h1.removeEventListener('mouseenter',alertH1);
// }
// h1.addEventListener('mouseenter', alertH1);
// setTimeout( () => HTMLQuoteElement.removeEventListener(
// 'mouseenter', alertH1), 3000);
// h1.onmouseenter = function(e) {
// alert('addEventListener: Great! ')
// };
// Event Propagation: Bubbling and Capturing
// Event Progpagation Practice
const randomInt = (min,max) => Math.floor(Math.random() * (max-min+1) + min);
const randomColor = () => `rgb(${randomInt(0,255)}, ${randomInt(0,255)}, ${randomInt(0,255)} )`;
// document.querySelector('.nav__link').addEventListener('click', function(e) {
// this.style.backgroundColor = randomColor();
// console.log('LINK', e.target,e.currentTarget);
// console.log(e.currentTarget === this);
// // stop propagation
// // e.stopPropagation();
// })
// document.querySelector('.nav__links').addEventListener('click', function(e) {
// this.style.backgroundColor = randomColor();
// console.log('Container', e.target,e.currentTarget);
// })
// document.querySelector('.nav').addEventListener('click', function(e) {
// this.style.backgroundColor = randomColor();
// console.log('nav', e.target,e.currentTarget);
// },);
// DOM traversing
const h1 = document.querySelector('h1');
// going downwards: child
console.log(h1.querySelectorAll('.highlight'));
console.log(h1.childNodes);
console.log(h1.children);
h1.firstElementChild.style.color = 'white';
h1.lastElementChild.style.color = 'blue';
//going upwards: parents
console.log(h1.parentNode);
console.log(h1.parentElement);
// selects closes parent based on class name
h1.closest('.header').style.background = 'var(--gradient-secondary)'
// .closest is like the oppoiste of querySelector
h1.closest('h1').style.background = 'var(--gradient-primary)'
// Going sideways: Siblings
console.log(h1.previousElementSibling);
console.log(h1.nextElementSibling);
console.log(h1.previousSibling);
console.log(h1.nextSibling);
//get all siblings
console.log(h1.parentElement.children);
[...h1.parentElement.children].forEach(function(el) {
if(el !== h1) {
el.style.transform = 'scale(0.5)';
}
});