-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall.js
150 lines (119 loc) · 4.36 KB
/
all.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
//上半部產品區塊
const containerProduct = document.querySelector(".container-product");
const bottle = document.querySelector(".bottle");
//行動版選單相關
const menuMobileBtn = document.querySelector(".menu__mobile__btn");
const menuMobileMain = document.querySelector(".menu-mobile-main");
//購物數量相關
const minus = document.querySelector(".minus");
const num = document.querySelector(".num");
const add = document.querySelector(".add");
const addToCart = document.querySelector(".addToCart");
//購物車icon相關
const cartIconCount = document.querySelector(".cart__count");
const cartIcon = document.querySelector(".cart");
const cartItem = document.querySelector(".cart__item");
const cartQuantity = document.querySelector(".cart__item__quantity");
const deleteBtn = document.querySelector(".delete__btn");
const overlay = document.querySelector(".overlay");
const subMenu = menuMobileMain.querySelector(".sub-menu");
const arrow = menuMobileMain.querySelector(".arrow");
//行動版漢堡選單點擊
menuMobileBtn.addEventListener("click", ()=>{
document.body.classList.toggle('no-scroll');
if(menuMobileMain.classList.contains('active')){
menuMobileMain.classList.remove("active");
}else{
menuMobileMain.classList.add("active");
}
cartItem.classList.remove("active");
overlay.classList.remove("active"); //頁面透黑色遮蓋
})
//行動版選單項目點擊
menuMobileMain.addEventListener("click", (e) => {
if(e.target.tagName == "A"){
subMenu.classList.add("active");
}
})
arrow.addEventListener("click", () => {
subMenu.classList.remove("active");
})
// 加入購物車數量計算
let number = 1;
minus.addEventListener("click", ()=>{
click(-1);
check();
});
add.addEventListener("click", ()=>{
click(1);
check();
});
function click(counting){
number = number + counting;
num.textContent = number;
};
//加減數字同步購物車icon+檢查數字最小為1
function check(){
if(num.textContent < 1){
number = 1; //最小等於1,不可以小於1
num.textContent = 1;
}
}
// 點擊加入購物車按鈕,右上角購物車 icon 就累計數字
cartIconCount.textContent = 0;
addToCart.addEventListener("click", ()=>{
cartIconCount.textContent = parseInt(cartIconCount.textContent) + parseInt(num.textContent);
cartQuantity.textContent = cartIconCount.textContent;
if(parseInt(cartIconCount.textContent) > 0){
cartIconCount.style.backgroundColor = "#C84601"; //數量大於0時數字背景變橘紅色
}
})
//點擊購物車 icon:當選購數字大於0時才顯示購物車清單
let array = Array.from(cartItem.children);
cartIcon.addEventListener("click", ()=>{
if(parseInt(cartIconCount.textContent) > 0){
cartItem.classList.toggle("active");
array.forEach(item=>{
item.style.opacity = 1;
});
//頁面透黑色遮蓋
if(overlay.classList.contains('active')){
overlay.classList.remove("active");
}else{
overlay.classList.add("active");
};
//購物車清單點擊顯示時,漢堡遠單就要隱藏,body恢復可以捲動
if(menuMobileMain.classList.contains('active')){
menuMobileMain.classList.remove("active");
document.body.classList.toggle('no-scroll');
};
}
})
//點擊購物車產品刪除按鈕
deleteBtn.addEventListener("click", ()=>{
cartItem.classList.toggle("active");
cartIconCount.textContent = 0;
cartQuantity.textContent = cartIconCount.textContent;
array.forEach(item=>{
item.style.opacity = 0;
});
if(overlay.classList.contains('active')){
overlay.classList.remove("active"); //頁面透黑色遮蓋
}else{
overlay.classList.add("active");
}
cartIconCount.style.backgroundColor = "grey";
});
// 點擊抓取 bottle 實際高度
// 用 bottle 的高度當作 photos 的 margin-top 的負值
bottle.addEventListener("click", function(e){
const rect = this.getBoundingClientRect().height;
console.log(rect);
//回傳 453.40625
//clientHeight回傳的是整數,不夠精準
});
// 監聽視窗尺寸變動
// window.addEventListener("resize", function(){
// let bottleH = bottle.getBoundingClientRect().height;
// photos.style.setProperty("margin-top", `-${bottleH}px`);
// })