-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts.js
119 lines (99 loc) · 4.38 KB
/
products.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
let cartCount = 0;
let cartItems = []; // Array to hold cart items
function addToCart(productName, productImage) {
cartCount++;
cartItems.push({ name: productName, image: productImage }); // Store product as an object with name and image
document.getElementById('cart-count').innerText = cartCount;
document.getElementById('cart-count-mobile').innerText = cartCount;
alert(`${productName} has been added to your cart!`);
}
function openProductModal(productName, productDescription) {
document.getElementById('modal-product-title').innerText = productName;
document.getElementById('modal-product-description').innerText = productDescription;
document.getElementById('product-modal').style.display = 'block'; // Show the product modal
}
function closeProductModal() {
document.getElementById('product-modal').style.display = 'none'; // Hide the product modal
}
function openCartModal() {
const cartModal = document.getElementById('cart-modal');
const cartItemsContainer = document.getElementById('cart-items');
const cartTotal = document.getElementById('cart-total');
// Clear previous cart items
cartItemsContainer.innerHTML = '';
// Display cart items with images
cartItems.forEach((item, index) => {
const itemElement = document.createElement('div');
itemElement.classList.add('cart-item');
// Create the image element
const itemImage = document.createElement('img');
itemImage.src = item.image; // Set the image source to the product's image
itemImage.alt = item.name; // Set alt text as the product name
itemImage.classList.add('cart-item-image');
// Create the name element
const itemName = document.createElement('span');
itemName.textContent = item.name;
// Create remove button
const removeButton = document.createElement('button');
removeButton.classList.add('remove-button');
removeButton.textContent = 'Remove';
removeButton.addEventListener('click', function() {
removeItemFromCart(index);
});
// Append image, name, and remove button to item
itemElement.appendChild(itemImage);
itemElement.appendChild(itemName);
itemElement.appendChild(removeButton);
cartItemsContainer.appendChild(itemElement); // Add item to the cart modal
});
cartTotal.textContent = `Total Items: ${cartCount}`;
cartModal.style.display = 'block'; // Show the modal
}
function closeCartModal() {
const cartModal = document.getElementById('cart-modal');
cartModal.style.display = 'none'; // Hide the modal
}
function removeItemFromCart(index) {
cartItems.splice(index, 1); // Remove the item from the array
cartCount--; // Decrease cart count
document.getElementById('cart-count').innerText = cartCount;
document.getElementById('cart-count-mobile').innerText = cartCount;
openCartModal(); // Refresh the cart modal to reflect changes
}
function clearCart() {
cartItems = [];
cartCount = 0;
document.getElementById('cart-count').innerText = cartCount;
document.getElementById('cart-count-mobile').innerText = cartCount;
openCartModal(); // Refresh the cart modal after clearing the cart
}
function checkout() {
alert("Proceeding to checkout!");
closeCartModal(); // Close modal after checkout
}
// Add event listener to the cart icon
document.querySelector('.cart-icon').addEventListener('click', openCartModal);
document.getElementById('cart-icon-mobileview').addEventListener('click', openCartModal);
// Scroll Animation
// Function to check if an element is in the viewport
function isElementInViewport(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
// Function to add the visible class to elements in the viewport
function animateOnScroll() {
const productCards = document.querySelectorAll('.product-card');
productCards.forEach(card => {
if (isElementInViewport(card)) {
card.classList.add('visible');
}
});
}
// Initial check and event listener for scroll
window.addEventListener('scroll', animateOnScroll);
window.addEventListener('load', animateOnScroll); // Check on page load