-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
79 lines (58 loc) · 2.19 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
'use strict';
/* navbar toggle*/
const navbar = document.querySelector("[data-navbar]");
const navbarLinks = document.querySelectorAll("[data-nav-link]");
const menuToggleBtn = document.querySelector("[data-nav-toggle-btn]");
menuToggleBtn.addEventListener("click", function (){
navbar.classList.toggle("active");
this.classList.toggle("active");
});
for(let i = 0; i < navbarLinks.length; i++){
navbarLinks[i].addEventListener("click", function (){
navbar.classList.toggle("active");
menuToggleBtn.classList.toggle("active");
});
}
/* header sticky & back to top*/
const header = document.querySelector("[data-header]");
const backTopBtn = document.querySelector("[data-back-top-btn]");
window.addEventListener("scroll", function (){
if (window.scrollY >= 100) {
header.classList.add("active");
backTopBtn.classList.add("active");
} else {
header.classList.remove("active");
backTopBtn.classList.remove("active");
}
});
/* search box toggle*/
const searchBtn = document.querySelector("[data-search-btn]");
const searchContainer = document.querySelector("[data-search-container]");
const searchSubmitBtn = document.querySelector("[data-search-submit-btn]");
const searchCloseBtn = document.querySelector("[data-search-close-btn]");
const searchBoxElems = [searchBtn, searchSubmitBtn, searchCloseBtn];
for(let i = 0; i < searchBoxElems.length; i++) {
searchBoxElems[i].addEventListener("click", function (){
searchContainer.classList.toggle("active");
document.body.classList.toggle("active");
});
}
/**
* move cycle on scroll
*/
const deliveryBoy = document.querySelector("[data-delivery-boy]");
let deliveryBoyMove = -80;
let lastScrollPos = 0;
window.addEventListener("scroll", function(){
let deliveryBoyTopPos = deliveryBoy.getBoundingClientRect().top;
if (deliveryBoyTopPos < 500 && deliveryBoyTopPos > -250){
let activeScrollPos = window.scrollY;
if (lastScrollPos < activeScrollPos){
deliveryBoyMove++;
} else {
deliveryBoyMove--;
}
lastScrollPos = activeScrollPos;
deliveryBoy.style.transform = `translate(${deliveryBoyMove}px)`;
}
});