Skip to content

Instantly share code, notes, and snippets.

@FlySquare
Created June 11, 2022 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlySquare/fa670568a88b0daee82a7d30eebb7227 to your computer and use it in GitHub Desktop.
Save FlySquare/fa670568a88b0daee82a7d30eebb7227 to your computer and use it in GitHub Desktop.
Shoping page design
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Elite Store</title>
<link rel="shortcut icon" href="https://i.ibb.co/kQT3jT3/Logo-Makr-3tp-HXE.png" type="image/x-icon" />
<link rel="stylesheet" href="./css/style.css" />
<!-- Latest compiled and minified CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<!-- google Montserrat font -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;500&display=swap"
rel="stylesheet"
/>
<!-- Font awesome cdn-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" />
</head>
<body>
<header></header>
<main class="main">
<div class="custom-container">
<div class="search-field">
<div class="search-box">
<input id="input-field" class="search-field" type="text" placeholder="Search our product" />
<button
type="button"
class="btn search-button"
data-toggle="button"
aria-pressed="false"
autocomplete="off"
id="search-btn"
onclick="loadProducts()"
>
Search
</button>
</div>
</div>
<div class="row">
<div class="col-md-9 col-lg-9 col-sm-7 order-sm-last order-last order-md-first">
<div id="all-products" class="row row-cols-1 row-cols-lg-3 row-cols-md-2 g-3"></div>
</div>
<div class="col-md-3 col-lg-3 col-sm-5 cart-main order-first">
<div class="cart" id="my-cart">
<table class="table">
<thead>
<h1>My-Cart</h1>
<tr>
<th scope="col"></th>
<th scope="col"></th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr>
<th>Total Added-Products:</th>
<td><span id="total-Products">0</span></td>
<td></td>
<td></td>
</tr>
<tr>
<th>Price:</th>
<td>$ <span id="price">0</span></td>
<td></td>
<td></td>
</tr>
<tr>
<th>Delivery-Charge:</th>
<td>$ <span id="delivery-charge">20</span></td>
<td></td>
<td></td>
</tr>
<tr>
<th>Total-Tax:</th>
<td>$ <span id="total-tax">0</span></td>
<td></td>
<td></td>
</tr>
<tr>
<th scope="row">Total</th>
<td colspan="2">$ <span id="total">0</span></td>
<td></td>
</tr>
</tbody>
</table>
<div class="parces text-center">
<button class="card-btn-1">Buy Now</button>
</div>
</div>
</div>
</div>
</div>
</main>
<footer></footer>
<!-- add javascript file -->
<script src="./js/app.js"></script>
</body>
</html>
const loadProducts = () => {
const url = `https://raw.githubusercontent.com/ProgrammingHero1/ranga-store-api/main/ranga-api.json`;
fetch(url)
.then((response) => response.json())
.then((data) => showProducts(data));
};
loadProducts();
// show all product in UI
const showProducts = (products) => {
const allProducts = products.map((pd) => pd);
for (const product of allProducts) {
const image = product.image;
const div = document.createElement("div");
div.classList.add("col");
div.innerHTML = `
<div class="card h-100">
<div class="c-head">
<img class="product-image" src=${image}></img>
</div>
<div class="card-body">
<h5 class="card-title">${product.title}</h5>
<p class="card-category">${product.category}</p>
</div>
<div class="card-footer">
<h3 class="product-price">Price: $${product.price}</h3>
<p><i class="fas fa-star"></i> ${product.rating.rate} (${product.rating.count} customer reviews)</p>
<button onclick="addToCart(${product.id},${product.price})" id="addToCart-btn" class="card-btn-1">Add to cart</button>
<button id="details-btn" class="card-btn-2">Details</button>
</div>
</div>
`;
document.getElementById("all-products").appendChild(div);
}
};
let count = 0;
const addToCart = (id, price) => {
count = count + 1;
updatePrice("price", price);
updateTaxAndCharge();
document.getElementById("total-Products").innerText = count;
};
const getInputValue = (id) => {
const element = document.getElementById(id).innerText;
const converted = parseFloat(element);
return converted;
};
// main price update function
const updatePrice = (id, value) => {
const convertedOldPrice = getInputValue(id);
const convertPrice = parseFloat(value);
const subTotal = convertedOldPrice + convertPrice;
const total = subTotal.toFixed(2);
document.getElementById(id).innerText = total;
};
// set innerText function
const setInnerText = (id, value) => {
document.getElementById(id).innerText = value.toFixed(2);
};
// update delivery charge and total Tax
const updateTaxAndCharge = () => {
const priceConverted = getInputValue("price");
if (priceConverted > 200) {
setInnerText("delivery-charge", 30);
setInnerText("total-tax", priceConverted * 0.2);
}
if (priceConverted > 400) {
setInnerText("delivery-charge", 50);
setInnerText("total-tax", priceConverted * 0.3);
}
if (priceConverted > 500) {
setInnerText("delivery-charge", 60);
setInnerText("total-tax", priceConverted * 0.4);
}
updateTotal();
};
//grandTotal update function
const updateTotal = () => {
const grandTotal = getInputValue("price") + getInputValue("delivery-charge") + getInputValue("total-tax");
const finalGrandTotal = grandTotal.toFixed(2);
document.getElementById("total").innerText = finalGrandTotal;
};
body {
overflow-x: hidden;
font-family: "Montserrat", sans-serif;
background-color: #f2f2f2 !important;
}
.custom-container {
width: 100%;
padding: 20px;
max-width: 1365px;
margin: 0 auto;
}
/* Search box */
.search-box {
margin: 40px auto;
max-width: 100%;
width: 400px;
display: flex;
border: 1px solid #222;
}
#input-field {
width: calc(100% - 80px);
padding: 10px 15px;
border: none;
margin-right: -5px;
color: #333;
}
#input-field:focus {
outline: none;
}
#search-btn {
border-radius: 0;
padding: 0 18px;
background-color: white;
}
#search-btn:focus {
box-shadow: none;
}
/* Product Card */
.card {
background-color: #f7f7f7 !important;
border-radius: none !important;
transition: all 0.3s;
}
.card:hover {
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
transform: translateY(-4px);
}
.c-head {
height: 300px;
text-align: center;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
background-color: #ffffff;
}
.c-head img {
max-width: 100%;
max-height: 100%;
transform: scale(0.9);
transition: 0.2s;
}
.card .c-head:hover img {
transform: scale(0.95);
}
.card-body {
padding-bottom: 0 !important;
}
.card-footer {
border-top: none !important;
padding: 0px 1rem 18px !important;
background-color: transparent !important;
}
.card-title {
margin-bottom: 0.5rem;
font-size: 18px;
font-weight: 600;
color: #222;
}
.card-category {
text-transform: capitalize;
font-size: 15px;
font-weight: 500;
color: #333;
margin-bottom: 5px;
}
.product-price {
font-size: 22px;
font-weight: 600;
color: #333;
}
.card-btn-1 {
background-color: #282828;
color: #fff;
width: 50%;
padding: 10px;
border: none;
transition: 0.3s;
}
.card-btn-1:hover {
background-color: #4c4c4c;
}
.card-btn-2 {
border: 1px solid #282828;
padding: 9px;
background-color: white;
width: 48%;
max-width: 100%;
transition: 0.3s;
}
.card-btn-2:hover {
background-color: #4c4c4c;
color: #ffffff;
}
/* Cart design */
.cart {
width: 350px;
padding: 10px;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
border-radius: 5px;
margin-right: 30px;
position: fixed;
background-color: white;
}
.cart-main {
padding-right: 30px;
}
.fa-star {
color: #f7bf33;
}
/* Responsive css */
@media only screen and (max-width: 768px) {
.cart {
width: 300px;
padding: 10px;
border-radius: 5px;
margin-right: 30px;
position: fixed;
}
.cart-main {
padding-right: 10px;
}
}
@media only screen and (max-width: 568px) {
.cart {
width: 100%;
margin-bottom: 20px;
position: unset;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment