Certainly! To add a subtotal to the cart container, we'll compute the total price of all items in the cart and display it in the cart popup. Below is the modified code to include this functionality:
Updated Code with Subtotal Calculation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Music Website</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
text-align: center;
}
.hero {
height: 300px;
display: flex;
align-items: center;
justify-content: space-between;
background: white;
color: black;
font-size: 2rem;
padding: 0 20px;
}
.logo {
font-weight: bold;
font-size: 2rem;
text-transform: uppercase;
font-family: Impact, sans-serif;
}
.search-bar {
flex-grow: 1;
text-align: center;
}
.search-bar input {
width: 50%;
padding: 10px;
font-size: 1rem;
}
.buttons {
display: flex;
gap: 15px;
}
.buttons button {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 15px;
border: 2px solid black;
border-radius: 25px;
background: white; /* Button color */
font-size: 1rem;
cursor: pointer;
}
.buttons button:hover {
background: #f0f0f0; /* Hover effect */
}
nav {
padding: 20px;
}
nav a {
margin: 0 15px;
text-decoration: none;
color: #666;
font-size: 1rem;
font-weight: 300;
position: relative;
}
nav a::after {
content: '';
display: block;
width: 100%;
height: 2px;
background: #666;
position: absolute;
bottom: -2px;
left: 0;
transform: scaleX(0);
transition: transform 0.3s ease;
}
nav a:hover::after,
nav a.active::after {
transform: scaleX(1);
}
.content {
display: grid;
grid-template-columns: 1fr 3fr 1fr;
gap: 20px;
padding: 20px;
}
.tracks {
display: flex;
flex-direction: column;
gap: 10px;
align-items: center;
}
.track {
display: flex;
align-items: center;
justify-content: space-between;
width: 80%;
padding: 10px 0;
border-bottom: 1px solid #ccc;
text-align: left;
position: relative;
}
.play-button {
width: 30px;
height: 30px;
background: black;
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
margin-right: 10px;
font-size: 16px;
}
.track strong {
flex: 1;
text-align: left;
display: flex;
align-items: center;
gap: 10px;
}
.track small {
flex: 1;
text-align: center;
}
.track .track-price {
flex: 0;
text-align: right;
padding: 2px 6px;
border: 2px solid black;
border-radius: 10px;
font-weight: bold;
transition: background 0.3s ease, color 0.3s ease;
display: inline-block;
white-space: nowrap;
cursor: pointer;
position: relative;
}
.track .track-price:hover {
background: black;
color: white;
}
.dropdown {
display: none;
position: absolute;
right: 0;
top: 100%;
background: white;
border: 1px solid #ccc;
padding: 5px;
border-radius: 5px;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
}
.dropdown button {
background: white; /* Original button color */
color: black;
border: 2px solid black;
padding: 5px 10px;
cursor: pointer;
border-radius: 5px;
}
/* Cart Popup Styles */
.cart-popup {
display: none; /* Initially hidden */
position: absolute; /* Keep the popup within the parent */
top: 60px; /* Position under the button */
right: 0; /* Align with the button */
background: white;
border: 2px solid black; /* Matching border */
padding: 20px;
width: 300px; /* Increased width */
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
z-index: 1000;
border-radius: 5px; /* Rounded corners */
}
.cart-popup h2 {
margin: 0 0 10px;
}
.cart-item {
display: flex;
justify-content: space-between;
padding: 5px 0;
}
.cart-buttons {
display: flex;
justify-content: space-between;
margin-top: 10px;
}
.cart-buttons button {
background: white; /* Original styling */
color: black;
border: 2px solid black;
padding: 10px;
cursor: pointer;
border-radius: 5px;
transition: background 0.3s ease;
}
.cart-buttons button:hover {
background: #f0f0f0; /* Hover effect */
}
.subtotal {
font-weight: bold;
margin-top: 10px;
}
</style>
<script>
let cartItems = [];
function toggleDropdown(event) {
event.stopPropagation();
let dropdown = event.currentTarget.querySelector('.dropdown');
dropdown.style.display = dropdown.style.display === 'block' ? 'none' : 'block';
}
function toggleCart(event) {
event.stopPropagation(); // Prevent click from closing the popup
const cartPopup = document.getElementById('cart-popup');
cartPopup.style.display = cartPopup.style.display === 'block' ? 'none' : 'block';
displayCartItems();
}
function displayCartItems() {
const cartContent = document.getElementById('cart-content');
cartContent.innerHTML = ''; // Clear previous content
let subtotal = 0; // Initialize subtotal
cartItems.forEach((item, index) => {
const itemDiv = document.createElement('div');
itemDiv.className = 'cart-item';
itemDiv.innerHTML = `${item.name} <span>$${item.price}</span> <button onclick="removeItem(${index})">Remove</button>`;
cartContent.appendChild(itemDiv);
subtotal += item.price; // Add price to subtotal
});
// Display subtotal
const subtotalDiv = document.createElement('div');
subtotalDiv.className = 'subtotal';
subtotalDiv.innerHTML = `Subtotal: $${subtotal.toFixed(2)}`; // Format to two decimal places
cartContent.appendChild(subtotalDiv);
}
function checkout() {
alert('Proceeding to checkout...');
// Add your checkout logic here
}
function removeItem(index) {
cartItems.splice(index, 1);
displayCartItems();
}
document.addEventListener('click', (event) => {
const cartPopup = document.getElementById('cart-popup');
if (!cartPopup.contains(event.target) && event.target.closest('.buttons') === null) {
cartPopup.style.display = 'none';
}
document.querySelectorAll('.dropdown').forEach(dropdown => {
dropdown.style.display = 'none';
});
});
// Sample items for demonstration
function addToCart(item) {
cartItems.push(item);
}
// Sample data for cart
addToCart({ name: 'Track Title 1', price: 1.23 });
addToCart({ name: 'Track Title 2', price: 1.23 });
</script>
</head>
<body>
<div class="hero">
<div class="logo">Tha Hip Hop Collective</div>
<div class="search-bar">
<input type="text" placeholder="Search...">
</div>
<div class="buttons">
<button><i class="fas fa-user"></i> Account</button>
<button onclick="toggleCart(event)"><i class="fas fa-shopping-cart"></i> Cart</button>
</div>
</div>
<nav>
<a href="#tracks">Tracks</a>
<a href="#beats">Beats</a>
<a href="#about">About</a>
</nav>
<div class="content">
<div></div> <!-- Left Column (Empty) -->
<div class="tracks" id="tracks">
<div class="track">
<strong><div class="play-button">▶</div>Track Title 1</strong>
<small>3:45</small>
<div class="track-price" onclick="toggleDropdown(event)">$1.23
<div class="dropdown">
<button onclick="addToCart({ name: 'Track Title 1', price: 1.23 })">Buy</button>
</div>
</div>
</div>
<div class="track">
<strong><div class="play-button">▶</div>Track Title 2</strong>
<small>4:12</small>
<div class="track-price" onclick="toggleDropdown(event)">$1.23
<div class="dropdown">
<button onclick="addToCart({ name: 'Track Title 2', price: 1.23 })">Buy</button>
</div>
</div>
</div>
</div>
<div class="ad-space">Ad</div>
</div>
<!-- Cart Popup -->
<div id="cart-popup" class="cart-popup">
<h2>Cart Items</h2>
<div id="cart-content"></div> <!-- Cart Items will be displayed here -->
<div class="cart-buttons">
<button onclick="checkout()">Checkout</button>
</div>
</div>
</body>
</html>
Key Changes:
-
Subtotal Calculation:
- A
subtotal variable is introduced to keep track of the total price of items in the cart. As items are displayed in the cart, their prices are summed up.
-
Displaying the Subtotal:
- After all items are displayed in the cart, a new
div for the subtotal is created and appended to the cart content. The subtotal is formatted to two decimal places for better readability.
-
Styling for Subtotal:
- A new CSS class called
.subtotal has been added to style the subtotal for emphasis.
Result:
When you add items to the cart, the subtotal will be displayed just below the list of items. This change enhances the user experience by providing clear visibility on how much the items in the cart cost in total.
If you have any more requests or further adjustments, feel free to let me know!