-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
234 lines (217 loc) · 9.1 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
const body = document.querySelector("body"),
loader = document.querySelector(".loader-wrap"),
links = document.querySelectorAll('a[href="#"]'),
nav = document.querySelector("header nav"),
navToggle = document.querySelector("header nav .toggle"),
navSpanMiddle = document.querySelector("header nav .toggle .middle"),
navNavigationBar = document.querySelector("header nav .navigation-bar"),
navNavigationBarLi = document.querySelectorAll(
"header nav .navigation-bar li"
),
headerText = document.querySelector("header .text"),
headerSection = document.querySelector("header"),
aboutSection = document.querySelector(".about-us"),
recipeSection = document.querySelector(".recipes"),
menuSection = document.querySelector(".menu"),
fixedImageSection = document.querySelector(".fixed-image"),
footerSection = document.querySelector("footer"),
dotOne = document.querySelector(".dots .one"),
dotTwo = document.querySelector(".dots .two"),
dotThree = document.querySelector(".dots .three"),
dots = document.querySelectorAll(".dots > div"),
logoImage = document.querySelector("header nav .logo img"),
svgDown = document.querySelector("header .arrow-down"),
svgUp = document.querySelector(".copyright .arrow-up"),
menuImgs = document.querySelectorAll(".menu .menu-image-container img"),
boxModel = document.querySelector(".menu .box-model"),
menuImageContainer = document.querySelector(".menu-image-container"),
boxModelArrow = document.querySelector(".menu .box-model .arrow"),
boxModelImage = document.querySelector(".menu .box-model img"),
pageTitle = document.querySelector("title");
// remove loader
function fadeOutEffect() {
const fadeEffect = setInterval(function() {
if (!loader.style.opacity) {
loader.style.opacity = 1;
}
if (loader.style.opacity > 0) {
loader.style.opacity -= 0.4;
} else {
body.classList.remove('stop-scroll');
loader.classList.add('remove');
clearInterval(fadeEffect);
}
}, 100);
}
window.addEventListener("load", fadeOutEffect);
// prevent links click hash
links.forEach(link =>
link.addEventListener("click", function(e) {
e.preventDefault();
})
);
// toggle hamburger menu button
navToggle.addEventListener("click", () => {
navToggle.classList.toggle("active");
navSpanMiddle.classList.toggle("hide");
navNavigationBar.classList.toggle("show");
});
// show active navigationbar li
navNavigationBarLi.forEach(li =>
li.addEventListener("click", () => {
const arr = Array.from(li.parentElement.children);
arr.forEach(li => li.classList.remove("active"));
li.classList.add("active");
})
);
// svg-up smooth scroll
svgUp.addEventListener("click", () => {
window.scroll({
top: 0,
behavior: "smooth"
});
});
window.onscroll = function() {
// make navbar fixed & change logo color
if (window.pageYOffset > headerSection.offsetHeight - 75) {
nav.classList.add("active");
logoImage.src = "https://res.cloudinary.com/abdel-rahman-ali/image/upload/v1535988525/logo-rosa.png";
} else {
nav.classList.remove("active");
logoImage.src = "https://res.cloudinary.com/abdel-rahman-ali/image/upload/v1535988515/logo-rosa-white.png";
}
// header welcome fade out and in
if (window.pageYOffset > 0) {
headerText.style.opacity = -window.pageYOffset / 300 + 1;
}
// home page JS
if (pageTitle.text === "ROSA- Restaurant") {
//change dots background color
if (window.pageYOffset < headerSection.offsetHeight * 0.5) {
dots.forEach(dot => dot.classList.remove("black"));
dotTwo.classList.remove("active");
dotOne.classList.add("active");
} else if (
window.pageYOffset > headerSection.offsetHeight * 0.5 &&
window.pageYOffset < recipeSection.offsetTop * 0.72
) {
dots.forEach(dot => dot.classList.add("black"));
} else if (
window.pageYOffset > recipeSection.offsetTop * 0.75 &&
window.pageYOffset < menuSection.offsetTop * 0.81
) {
dots.forEach(dot => dot.classList.remove("black"));
dotOne.classList.remove("active");
dotThree.classList.remove("active");
dotTwo.classList.add("active");
} else if (
window.pageYOffset > menuSection.offsetTop * 0.81 &&
window.pageYOffset < fixedImageSection.offsetTop * 0.86
) {
dots.forEach(dot => dot.classList.add("black"));
dotThree.classList.remove("active");
dotTwo.classList.add("active");
} else if (
window.pageYOffset > fixedImageSection.offsetTop * 0.86 &&
window.pageYOffset < footerSection.offsetTop * 0.72
) {
dots.forEach(dot => dot.classList.remove("black"));
dotTwo.classList.remove("active");
dotThree.classList.add("active");
} else if (
window.pageYOffset > footerSection.offsetTop * 0.72 &&
window.pageYOffset < footerSection.offsetTop * 0.901
) {
dots.forEach(dot => dot.classList.add("black"));
} else if (window.pageYOffset > footerSection.offsetTop * 0.901) {
dots.forEach(dot => dot.classList.remove("black"));
}
}
};
// home page JS
if (pageTitle.text === "ROSA- Restaurant") {
// svg-down smooth scroll
svgDown.addEventListener("click", () => {
window.scroll({
top: aboutSection.offsetTop - 30,
behavior: "smooth"
});
});
// dots smooth scroll
dots.forEach(dot =>
dot.addEventListener("click", function() {
window.scrollTo({
top: document.querySelector(this.dataset.x).offsetTop - 100,
behavior: "smooth"
});
})
);
// show box model
menuImgs.forEach(img =>
img.addEventListener("click", function() {
const arr = Array.from(this.parentElement.parentElement.children);
arr.forEach(div => div.classList.remove("active"));
this.parentElement.classList.add("active");
boxModel.classList.add("active");
boxModelImage.src = this.src;
boxModelImage.classList.add("active");
body.classList.add("hide-scroll");
})
);
// box model functions
function boxModelFun(e) {
// close box model
if (
e.code === "Escape" ||
(e.target.tagName === "DIV" && !e.target.classList.contains("arrow")) ||
e.target.classList.contains("close")
) {
boxModel.classList.remove("active");
body.classList.remove("hide-scroll");
}
if (boxModel.classList.contains("active")) {
if (
e.code === "ArrowRight" ||
e.code === "ArrowLeft" ||
e.target.classList.contains("arrow-right") ||
e.target.classList.contains("arrow-left")
) {
const arr = Array.from(menuImageContainer.children);
const active = arr.find(div => div.classList.contains("active"));
// change box model image
if (
e.target.classList.contains("arrow-right") ||
e.code === "ArrowRight"
) {
if (active.nextElementSibling === null) {
active.parentElement.firstElementChild.classList.add("active");
boxModelImage.src =
active.parentElement.firstElementChild.firstElementChild.src;
} else {
active.nextElementSibling.classList.add("active");
boxModelImage.src = active.nextElementSibling.firstElementChild.src;
}
}
// change box model image
else if (
e.target.classList.contains("arrow-left") ||
e.code === "ArrowLeft"
) {
if (active.previousElementSibling === null) {
active.parentElement.lastElementChild.classList.add("active");
boxModelImage.src =
active.parentElement.lastElementChild.lastElementChild.src;
} else {
active.previousElementSibling.classList.add("active");
boxModelImage.src =
active.previousElementSibling.firstElementChild.src;
}
}
active.classList.remove("active");
}
}
}
window.addEventListener("keydown", boxModelFun);
window.addEventListener("click", boxModelFun);
boxModelArrow.addEventListener("click", boxModelFun);
}