-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
85 lines (67 loc) · 2.38 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**-------Preloader-------------**/
let mask = document.querySelector('.mask');
window.addEventListener('load', () => {
mask.classList.add('hide');
setTimeout(() => {
mask.remove();
}, 600);
});
/**-------Toggle button-------------**/
let body = document.querySelector('body');
let toggler = document.querySelector('.navbar-toggler');
let scrollToTop = document.querySelector('.scroll_top');
toggler.addEventListener('click', () => {
toggler.classList.toggle('active');
body.classList.toggle('locked');
});
/**-------Scroll To Top-------------**/
/**-------Fixed Navbar-------------**/
/**-------Navigation-------------**/
let header = document.querySelector('.header_main');
let navbar = document.querySelector('.navbar');
if(window.innerWidth <= 991){
let navbarHeight = header.querySelector('.container-fluid').clientHeight;
window.onscroll = function(){
if(window.pageYOffset >= header.clientHeight - navbarHeight){
header.classList.add('active');
scrollToTop.classList.add('active');
}
else{
header.classList.remove('active');
scrollToTop.classList.remove('active');
}
};
}
window.onscroll = function(){
if(window.pageYOffset >= header.clientHeight){
scrollToTop.classList.add('active');
navbar.classList.add('fixed');
}
else{
scrollToTop.classList.remove('active');
navbar.classList.remove('fixed');
}
};
scrollToTop.onclick = function(){
window.scrollTo(0, 0);
};
/*Nav*/
const navLinks = document.querySelectorAll('.nav-link[data-goto]');
if(navLinks.length > 0){
navLinks.forEach( function(item){
item.addEventListener('click', onNavLink);
});
function onNavLink(e){
const navLink = e.target;
/*Поверяем существует ли дата-атрибут и есть ли в нём ссылка*/
if(navLink.dataset.goto && document.querySelector(navLink.dataset.goto)){
e.preventDefault();
const goToBlock = document.querySelector(navLink.dataset.goto);
const goToBlockValue = goToBlock.getBoundingClientRect().top + pageYOffset;
window.scrollTo({
top: goToBlockValue,
behavior: 'smooth'
});
}
}
}