-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
65 lines (56 loc) · 2.06 KB
/
main.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
(function () {
const header = document.querySelector('.header');
window.onscroll = () => {
if(window.pageYOffset > 50) {
header.classList.add('header_active')
} else {
header.classList.remove('header_active')
}
};
}());
// Burger hendler
(function () {
const burgerItem = document.querySelector('.burger');
const menu = document.querySelector('.header__nav');
const menuCloseItem = document.querySelector('.header__nav-close-line');
burgerItem.addEventListener('click', () => {
menu.classList.add('header__nav_active');
});
menuCloseItem.addEventListener('click', () => {
menu.classList.remove('header__nav_active');
});
}());
// Scroll to anchors
(function () {
const smoothScroll = function (targetEl, duration) {
const headerElHeight = document.querySelector('.menu').clientHeight;
let target = document.querySelector(targetEl);
let targetPosition = target.getBoundingClientRect().top - headerElHeight;
let startPosition = window.pageYOffset;
let startTime = null;
const ease = function(t,b,c,d) {
t /= d / 2;
if (t < 1) return c / 2 * t * t + b;
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
};
const animation = function(currentTime){
if (startTime === null) startTime = currentTime;
const timeElapsed = currentTime - startTime;
const run = ease(timeElapsed, startPosition, targetPosition, duration);
window.scrollTo(0,run);
if (timeElapsed < duration) requestAnimationFrame(animation);
};
requestAnimationFrame(animation);
};
const scrollTo = function () {
const links = document.querySelectorAll('.js-scroll');
links.forEach(each => {
each.addEventListener('click', function () {
const currentTarget = this.getAttribute('href');
smoothScroll(currentTarget, 1000);
});
});
};
scrollTo();
}());