-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
144 lines (121 loc) · 4.22 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
// Sample product data
const products = [
{
name: 'Ceramic Tiles',
image: 'images/ceramic-tiles.jpg',
price: 25.99
},
{
name: 'Bricks',
image: 'images/bricks.jpg',
price: 15.99
},
{
name: 'Cement',
image: 'images/cement.jpg',
price: 10.99
},
{
name: 'Interior Wallpaper',
image: 'images/interior-wallpaper.jpg',
price: 35.99
}
];
// Sample recommended product data
const recommendedProducts = [
{
name: 'Wooden Flooring',
image: 'images/wooden-flooring.jpg',
price: 45.99
},
{
name: 'Plaster of Paris',
image: 'images/plaster-of-paris.jpg',
price: 12.99
},
{
name: 'Decorative Stones',
image: 'images/decorative-stones.jpg',
price: 29.99
}
];
document.addEventListener('DOMContentLoaded', () => {
const productContainer = document.getElementById('productContainer');
const recommendedProductContainer = document.getElementById('recommendedProductContainer');
const cartCounter = document.getElementById('cartCounter');
const cartModal = document.getElementById('cartModal');
const cartContent = document.getElementById('cartContent');
const totalAmount = document.getElementById('totalAmount');
let cart = [];
// Display products
function displayProducts(products, container) {
container.innerHTML = '';
products.forEach(product => {
const productItem = document.createElement('div');
productItem.classList.add('product-item');
const productImage = document.createElement('img');
productImage.src = product.image;
productImage.alt = product.name;
const productName = document.createElement('h3');
productName.textContent = product.name;
const productPrice = document.createElement('p');
productPrice.textContent = `$${product.price}`;
const addToCartBtn = document.createElement('button');
addToCartBtn.textContent = 'Add to Cart';
addToCartBtn.addEventListener('click', () => {
addToCart(product);
updateCart();
});
productItem.appendChild(productImage);
productItem.appendChild(productName);
productItem.appendChild(productPrice);
productItem.appendChild(addToCartBtn);
container.appendChild(productItem);
});
}
// Add product to cart
function addToCart(product) {
cart.push(product);
}
// Update cart
function updateCart() {
cartCounter.textContent = cart.length;
displayCart();
}
// Display cart
function displayCart() {
cartContent.innerHTML = '';
let total = 0;
cart.forEach(item => {
const cartItem = document.createElement('div');
cartItem.classList.add('cart-item');
const itemName = document.createElement('p');
itemName.textContent = item.name;
const itemPrice = document.createElement('span');
itemPrice.textContent = `$${item.price}`;
cartItem.appendChild(itemName);
cartItem.appendChild(itemPrice);
cartContent.appendChild(cartItem);
total += item.price;
});
totalAmount.textContent = total.toFixed(2);
}
// Display recommended products
displayProducts(products, productContainer);
displayProducts(recommendedProducts, recommendedProductContainer);
// Open cart modal
document.querySelector('nav ul li:nth-child(4)').addEventListener('click', () => {
cartModal.style.display = 'block';
updateCart();
});
// Close cart modal
document.querySelector('.close').addEventListener('click', () => {
cartModal.style.display = 'none';
});
// Click outside of cart modal to close
window.onclick = function(event) {
if (event.target == cartModal) {
cartModal.style.display = 'none';
}
};
});