-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
318 lines (262 loc) · 11.6 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
'use strict';
///////////////////////////////////////
// Modal window
const footer = document.querySelector('.footer');
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 header = document.querySelector('.header');
const cookieMessage = document.createElement('div');
const btnScrollTo = document.querySelector('.btn--scroll-to');
const section1 = document.querySelector('#section--1')
const section2 = document.querySelector('#section--2')
const section3 = document.querySelector('#section--3')
const tabs = document.querySelectorAll('.operations__tab')
const tabsContainer = document.querySelector('.operations__tab-container')
const operationsContents = document.querySelectorAll('.operations__content');
const logo = document.querySelector('.nav__logo')
cookieMessage.classList.add('cookie-message');
cookieMessage.innerHTML = `we use cookies for better analytics <button class="btn btn--close--cookie">OK</button>`;
function changeOpacity(e) {
if (!e.target.classList.contains('nav__link')) return;
// console.log('contains')
const closeNav = e.target.closest('.nav')
const siblings = closeNav.querySelectorAll('.nav__link');
const logo = closeNav.querySelector('.nav__logo');
const elements = [...siblings, logo]
elements.forEach((s) => {
// if(s!=e.target)
if (e.target !== s) s.style.opacity = this;
});
// logo.style.opacity=0.5;
// e.target.style.opacity=opacity
};
// document.querySelector('.nav').addEventListener('mouseover', e=>{
// if (!e.target.classList.contains('nav__link')) return;
// const closeNav = e.target.closest('.nav')
// const siblings =closeNav.querySelectorAll('.nav__link');
// const logo = closeNav.querySelector('.nav__logo');
// const elements =[...siblings, logo]
// elements.forEach((s)=>{
// // if(s!=e.target)
// s.style.opacity=0.5;
// });
// // logo.style.opacity=0.5;
// e.target.style.opacity=1
// });
// document.querySelector('.nav').addEventListener('mouseout', (e)=>{
// const interestElements=[...e.target.closest('.nav').querySelectorAll('.nav__link'), e.target.closest('.nav').querySelector('.nav__logo')];
// interestElements.forEach(el=> {el.style.opacity=1})
// })
footer.after(cookieMessage);
// header.insertAdjacentHTML('beforebegin', 'we use cookies for better analytics <button class="btn btn--close--cookie">OK</button>')
document.querySelector('.btn--close--cookie').addEventListener('click', () => {
cookieMessage.remove();
})
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');
};
const randomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1));
}
const generateRGB = () => {
return `rgba(${randomInt(0, 255)},${randomInt(0, 255)},${randomInt(0, 255)}, ${randomInt(0, 100) / 100})`
}
//slider:
function slider () {
const leftButton = document.querySelector('.slider__btn--left')
const rightButton = document.querySelector('.slider__btn--right')
const slides = document.querySelectorAll('.slide');
const dotsContainer = document.querySelector('.dots')
const createDots = () => {
slides.forEach((_, i) => {
dotsContainer.insertAdjacentHTML('beforeend', `<button class="dots__dot" data-slide="${i}"></button>`)
})
dotsContainer.addEventListener('click', (e)=>{
if (!e.target.classList.contains('dots__dot')) return;
const slideIndex = e.target.dataset.slide;
goToSlide(slideIndex)
})
}
let currentSlide = 0;
function goToSlide(number) {
currentSlide = number;
slides.forEach((elem, index) => (
elem.style.transform = `translateX(${(index-currentSlide) * 100}%)`
))
console.log(dotsContainer)
console.log(document.querySelectorAll('.dots__dot'))
document.querySelectorAll('.dots__dot').forEach(dot=>{dot.classList.remove('dots__dot--active')});
document.querySelector(`.dots__dot[data-slide="${number}"]`).classList.add('dots__dot--active')
}
const nextSlide = () => {
currentSlide++;
currentSlide %= slides.length;
goToSlide(currentSlide)
}
const prevSlide = () => {
currentSlide = (currentSlide === 0) ? slides.length - 1 : currentSlide - 1;
goToSlide(currentSlide)
}
createDots();
goToSlide(0);
leftButton.addEventListener('click', prevSlide)
rightButton.addEventListener('click', nextSlide)
}
slider();
// intersection observer API // scrolling effects:
const lazyImages = document.querySelectorAll('img[data-src]')
const lazyOptions = {
root: null,
threshold: 0,
rootMargin: '-100px', // before we even reach the img, let's load it, so the user can't notice we're lazy loading
}
const lazyCallback = (entries, observer) => {
const [entry] = entries;
if (!entry.isIntersecting) return;
console.log(entry)
entry.target.src = entry.target.dataset.src;
entry.target.addEventListener('load', () => { entry.target.classList.remove('lazy-img') })
lazyObserver.unobserve(entry.target);
}
const lazyObserver = new IntersectionObserver(lazyCallback, lazyOptions)
lazyImages.forEach(li => lazyObserver.observe(li));
//sticky navbar
// window.addEventListener('scroll', (e)=>{
// const navbar = document.querySelector('.nav')
// // window.scrollY>100 && navbar.classList.add('sticky')
// // window.scrollY<100 && navbar.classList.remove('sticky')
// // e.scrollY>100 && navbar.classList.add('sticky')
// // e.scrollY<100 && navbar.classList.remove('sticky')
// const section1coords = section1.getBoundingClientRect();
// // console.log(section1coords)
// if (window.scrollY>section1coords.top) {
// navbar.classList.add('sticky');
// // cookieMessage.classList.add('sticky');
// }
// else navbar.classList.remove('sticky')
// })
/// or we can use intersection observer API:
const navCallback = (entries, observer) => {
const [entry] = entries;
const nav = document.querySelector('.nav');
if (!entry.isIntersecting) nav.classList.add('sticky')
else nav.classList.remove('sticky')
}
const navOptions = {
root: null, //=> root=viewport
threshold: 0,
rootMargin: `-${document.querySelector('.nav').getBoundingClientRect().height}px` // endofheader-navbarheight
}
const navObserver = new IntersectionObserver(navCallback, navOptions);
navObserver.observe(header);
//reveal sections
document.querySelectorAll('.section').forEach(el => {
el.classList.add('section--hidden')
})
const opaOptions = { // options needed to change the opacity
root: null,
threshold: 0.5
}
const opaCallBack = (entries, observer) => { // callback for changing the opacity in given sections
const [entry] = entries
// console.log(entry)
// if (!entry.isIntersecting) return
entry.target.classList.remove('section--hidden')
observer.unobserve(entry.target)
// if (entry.isIntersecting) return;
// entry.target.classList.remove('section--hidden')
// entry.classList.remove('section--hidden')
}
const opacityObserver = new IntersectionObserver(opaCallBack, opaOptions);
document.querySelectorAll('.section').forEach(el => {
el.classList.add('section--hidden');
opacityObserver.observe(el)
})
document.querySelector('.nav').addEventListener('mouseover', changeOpacity.bind(0.5))
document.querySelector('.nav').addEventListener('mouseout', changeOpacity.bind(1));
tabsContainer.addEventListener('click', function (e) {
const triggerer = e.target.classList.contains('btn') ? e.target : e.target.closest('.operations__tab')
if (!triggerer) return;
const index = triggerer.getAttribute('data-tab')
console.log(index)
tabs.forEach((tab) => { tab.classList.remove('operations__tab--active') })
operationsContents.forEach(oc => { oc.classList.remove('operations__content--active') })
document.querySelector(`.operations__tab--${index}`).classList.add('operations__tab--active')
document.querySelector(`.operations__content--${index}`).classList.add('operations__content--active')
});
// document.querySelectorAll('.nav__link').forEach(el=>{
// el.addEventListener('click', function(e){ // with arrow fn one cannot use 'this', so let's use this kind of fn
// e.preventDefault();
// // const id = this.href;// it returns whole link (with http://local.....etc + #section--1)
// const id = this.getAttribute('href'); //-> it returns sth like '#section--1'. PERFECT SELECTOR. LET'S USE IT!
// document.querySelector(id).scrollIntoView({behavior :'smooth'})
// })
// })// here we create a lot of same functions, not the best solution. let's create one in parent element that knows
//who(which chilid of him) is the event trigger (e.target) and use function that uses particular elem in event handling:
document.querySelector('.nav__links').addEventListener('click', function (e) {
e.preventDefault();
const triggerer = e.target;
// console.log(triggerer);
if (!triggerer.classList.contains('nav__link')) return; // if it's not the kind of child we are looking for, don't continue the fn
const dest = triggerer.getAttribute('href');
// console.log(dest);
document.querySelector(dest).scrollIntoView({ behavior: 'smooth' })
})
//too much pain in the ass to work with socommented ;)
// document.querySelector('.header').addEventListener('click', function(){
// // console.log('.header')
// setInterval(()=>{ this.style.backgroundColor=`rgba(${randomInt(0,255)}, 124, 244, ${randomInt(0,100)/100})`}, 4000)
// }, true)
// document.querySelector('.nav').addEventListener('click', function(){
// setInterval(()=>{this.style.backgroundColor=`rgba(${randomInt(100, 120)}, ${randomInt(200, 120)}, ${randomInt(300, 120)}, ${randomInt(0,100)/100})`}, 2000)
// // console.log('.nav')
// }, true)
// document.querySelector('.nav__logo').addEventListener('click', function(e){
// setInterval(()=>{this.style.backgroundColor=generateRGB()}, 3500);
// e.stopPropagation();
// // console.log('.nav__logo')
// e.stopPropagation(); // stops the propagation (handling event in bubbling phase where e.target!==e.currentTarget)
// })
btnScrollTo.addEventListener('click', (e) => {
// const scrollX = window.scrollX;
// const scrollY = window.scrollY;
// const clientWidth = document.documentElement.clientWidth;
// const clientHeight = document.documentElement.clientHeight;
// const section1coords = e.target.getBoundingClientRect();
// console.log(section1coords)
// console.log(e.target.getBoundingClientRect())
// window.scrollTo(section1coords.left + scrollX, section1coords.top+ scrollY) or if you want to do it smoothly, u have to pass
//an object with 'left', 'top', and 'behavior' properties/attributes
// window.scrollTo({
// left: section1coords.left + scrollX,
// top: section1coords.top+ scrollY,
// behavior: 'smooth',
// })
//or even more modern approach using method:
section1.scrollIntoView({ behavior: 'smooth' })
})
btnsOpenModal.forEach(mod => {
mod.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();
}
});
//editing styles:
document.documentElement.style.setProperty('--color-primary', 'orangered');
//example below gets fontsize (e.g. '20px') from getComputedStyle, so we have to take only the number part (parseFloat)
// and then edit the number value so we can later add 'px';
cookieMessage.style.fontSize = parseFloat(getComputedStyle(cookieMessage).fontSize) + 20 + 'px';
//editing attributes:
//const logo = document.querySelector('.nav__logo');