-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·298 lines (235 loc) · 9 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
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
'use strict'
// selectors
const header = document.querySelector('header');
const nav = document.querySelector('nav');
// Menu selectors
const navLinks = document.querySelector('nav .links');
// Modal window Selectors
const modal = document.querySelector('.modal');
const btnCloseModal = document.querySelector('.btn--close-modal');
const btnsOpenModal = document.querySelectorAll('.btn--show-modal');
const overlay=document.querySelector(".overlay");
// ! Modal Window 🪟
const openModal = function () {
modal.classList.remove('hidden');
overlay.classList.remove('hidden');
overlay.classList.remove('show-mobile');
document.body.style.overflow = 'hidden';
};
const closeModal = function () {
modal.classList.add('hidden');
overlay.classList.add('hidden');
document.body.style.overflow = 'auto';
};
btnsOpenModal.forEach((btn) => btn.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();
if (e.key == 'Escape' && !navLinks.classList.contains('off-mobile'))
closeMenu();
});
//creating
// Add cookie Message 🍪🍪🍪
// ! First Create it
const cookieMessage = document.createElement('div');
cookieMessage.innerHTML = `We Use Cookie🍪 for Improved Functionality and analytics <button class='btn btn--close--cookie'>Got It</button>`
cookieMessage.classList.add('cookie-message')
// console.log(cookieMessage);
// ! Then Insert it at header
// insertAdjacentHtml [before begin, after begin, before end, after end]
// header.insertAdjacentElement('beforeend', cookieMessage)
// you can also use these methods [before, prepend, append, after]
document.querySelector('header .hero ').append(cookieMessage);
// ! removing it on click
document.querySelector('.btn--close--cookie')
.addEventListener('click', function () {
cookieMessage.remove();
})
// Style
// it is an inline style
cookieMessage.style.backgroundColor = '#37383d';
// console.log(cookieMessage.style.height);
// will print nothing because height is not inline style
// solution ⭐⭐⭐ getComputed
// console.log(getComputedStyle(cookieMessage).height);
cookieMessage.style.height =
Number.parseFloat(getComputedStyle(cookieMessage).height)
+ 30 + 'px';
//
// If you clicked
// 🔗🔗🔗 Page Navigation My code
// learn more btn-scroll
document
.querySelector('.btn-scroll')
.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector('.features').scrollIntoView({ behavior: 'smooth' })
})
// document.querySelectorAll('nav .links li a').forEach(function (el) {
// el.addEventListener('click', function (e) {
// e.preventDefault();
// // select element I want to scroll to
// const sec = document.querySelector(this.getAttribute('href'))
// sec.scrollIntoView({ behavior: 'smooth' });
// })
// })
// In the above code you've create a function for each element which will fuck the performance
// sol : Event Delegation ⭐⭐⭐
navLinks.addEventListener('click', function (e) {
e.preventDefault();
if (e.target.classList.contains('nav_link')) {
const sec = document.querySelector(e.target.getAttribute('href'))
sec.scrollIntoView({ behavior: 'smooth' });
}
})
// Operations tab ⭐⭐⭐⭐
// get buttons using event delegation
document
.querySelector('.operations__tab-container')
.addEventListener('click', function (e) {
const clicked = e.target.closest('.operations__tab');// so If I clicked on child I get the tab also
if (!clicked) return;
const curContentActive = document.querySelector('.operations .operations__content--active');
const curTabActive = clicked.parentNode.querySelector('.operations__tab--active');
const targetContentActive = curContentActive.parentNode.querySelector(`.operations__content--${clicked.dataset.tab}`);
if (clicked !== curTabActive) {
curContentActive.classList.remove('operations__content--active')
curTabActive.classList.remove('operations__tab--active')
targetContentActive.classList.add('operations__content--active')
clicked.classList.add('operations__tab--active')
}
});
// Menu fading when hover on one of the links 🍪🍪🍪🍪
const changeOpacity = function (e, opacity) {
if (e.target.classList.contains('nav_link')) {
const targetLink = e.target;
const siblings = targetLink.closest('.links').querySelectorAll('a');
const logo = targetLink.closest('.container').querySelector('img');
logo.style.opacity = this;
siblings.forEach((link) => {
if (link !== targetLink) link.style.opacity = this;
})
}
}
document.querySelector('nav .container')
.addEventListener('mouseover', changeOpacity.bind(.5));
document.querySelector('nav .container')
.addEventListener('mouseout', changeOpacity.bind(1));
// Sticky navigation : Intersection Observer API
const stickyNav = function (entries) {
const [entry] = entries;
// console.log(entry);
if (entry.isIntersecting)
nav.classList.remove('sticky');
else
nav.classList.add('sticky');
}
const navHeight = nav.getBoundingClientRect().height;
const obsOptions = {
root: null,
threshold: 0,
rootMargin: `-${navHeight}px`,
}
const headerObserver = new IntersectionObserver(stickyNav, obsOptions);
headerObserver.observe(document.querySelector('header'));
// My entrie isIntersecting=true if my intersection with the viewport is >=threshold
// Sections Revealing
const allSections = document.querySelectorAll('section');
allSections.forEach((ele) => ele.classList.add('section-hidden'))
const makeVisible = function (entries, observer) {
const [entry] = entries;
// console.log(entry.target);
if (!entry.isIntersecting) return;
entry.target.classList.remove('section-hidden');
observer.unobserve(entry.target);
}
const obsSectionOptions = {
root: null,
threshold: .15,
}
const sectionObserver = new IntersectionObserver(makeVisible, obsSectionOptions);
allSections.forEach((ele) => sectionObserver.observe(ele))
// ! Lazy loading Images
const allImages = document.querySelectorAll('img.feature-img');
const enhanceImage = function (entries, observer) {
const [entry] = entries;
// console.log(entry);
if (!entry.isIntersecting) return;
if (!entry.target.classList.contains('lazy-img')) return;
entry.target.src = entry.target.dataset.src;
// to make it remove filter after reloading img
entry.target.addEventListener('load', function () {
entry.target.classList.remove('lazy-img');
})
observer.unobserve(entry.target);
}
const obsImgOptions = {
root: null,
threshold: 0,
rootMargin: '-200px'
}
const imgObserver = new IntersectionObserver(enhanceImage, obsImgOptions);
allImages.forEach((image) => imgObserver.observe(image));
// Slider 🛝🛝🛝
const allSlides = document.querySelectorAll('.slide');
allSlides.forEach((sld, i) => {
sld.style.transform = `translateX(${i * 100}%)`;
});
let curSlide = 0;
const n = allSlides.length;
const slidBtnLeft = document.querySelector('.slider__btn--left');
const slideBtnRight = document.querySelector('.slider__btn--right');
const goToSlide = function (slide) {
allSlides.forEach((sld, i) => {
let val = 100 * (i - slide);
sld.style.transform = `translateX(${val}%)`;
});
}
goToSlide(0);
const prevSlide = function () {
if (curSlide === 0)
curSlide = n - 1;
else curSlide--;
goToSlide(curSlide);
}
const nextSlide = function () {
if (curSlide === n - 1) curSlide = 0;
else curSlide++;
goToSlide(curSlide)
}
slidBtnLeft.addEventListener('click', prevSlide);
slideBtnRight.addEventListener('click', nextSlide);
document.addEventListener('keydown', function (e) {
if (e.key === 'ArrowLeft') prevSlide();
else if (e.key === 'ArrowRight') nextSlide();
})
// LEC ## Dom Traversing
// const h1 = document.querySelector(".hero .container h1");
// // DownWards select childs
// // for direct children only
// console.log(h1.childNodes);
// console.log(h1.children);
// h1.firstElementChild.style.color = "orangered";
// h1.lastElementChild.style.color = "blue";
// // upwards for parent
// console.log(h1.parentElement);
// [...h1.parentElement.children].forEach(ele => {
// if (ele !== h1) {
// ele.style.backgroundColor = "#262a33";
// console.log("hello");
// }
// })
// setProperty
// document.documentElement.style.setProperty('--color-primary', 'red');
// const logo = document.querySelector('.logo')
// // Attributes
// console.log(logo.className);
// // non-standard
// console.log(logo.nonStandard);// undefined
// // getAttribute => returns it as written in html
// console.log(logo.getAttribute('non-standard'));// will get easily
// // dataset
// console.log(logo.dataset.version);// data-version
//