├── README.md ├── custom.js ├── images ├── cart.png ├── favicon.svg ├── logoShop.png └── products │ ├── keyboard-1.jpg │ ├── keyboard-2.jpg │ ├── keyboard-3.jpg │ ├── keyboard-4.jpg │ └── keyboard-5.jpg ├── index.html └── style.css /README.md: -------------------------------------------------------------------------------- 1 | # 🛒Carrito-de-Compras-JavaScript 2 | ![image](https://drive.google.com/uc?export=view&id=1mgF10d5yVdjBh1rfwrPBvKYhHPQ349FW) 3 | 4 | ## 🚀DEMO 5 | [Deploy](https://carrito-de-compras-js.vercel.app/) 6 | 7 | ## 💻Tutorial 8 | 9 | Carrito de compras video 10 | 11 | -------------------------------------------------------------------------------- /custom.js: -------------------------------------------------------------------------------- 1 | //variables 2 | let allContainerCart = document.querySelector('.products'); 3 | let containerBuyCart = document.querySelector('.card-items'); 4 | let priceTotal = document.querySelector('.price-total') 5 | let amountProduct = document.querySelector('.count-product'); 6 | 7 | 8 | let buyThings = []; 9 | let totalCard = 0; 10 | let countProduct = 0; 11 | 12 | //functions 13 | loadEventListenrs(); 14 | function loadEventListenrs(){ 15 | allContainerCart.addEventListener('click', addProduct); 16 | 17 | containerBuyCart.addEventListener('click', deleteProduct); 18 | } 19 | 20 | function addProduct(e){ 21 | e.preventDefault(); 22 | if (e.target.classList.contains('btn-add-cart')) { 23 | const selectProduct = e.target.parentElement; 24 | readTheContent(selectProduct); 25 | } 26 | } 27 | 28 | function deleteProduct(e) { 29 | if (e.target.classList.contains('delete-product')) { 30 | const deleteId = e.target.getAttribute('data-id'); 31 | 32 | buyThings.forEach(value => { 33 | if (value.id == deleteId) { 34 | let priceReduce = parseFloat(value.price) * parseFloat(value.amount); 35 | totalCard = totalCard - priceReduce; 36 | totalCard = totalCard.toFixed(2); 37 | } 38 | }); 39 | buyThings = buyThings.filter(product => product.id !== deleteId); 40 | 41 | countProduct--; 42 | } 43 | //FIX: El contador se quedaba con "1" aunque ubiera 0 productos 44 | if (buyThings.length === 0) { 45 | priceTotal.innerHTML = 0; 46 | amountProduct.innerHTML = 0; 47 | } 48 | loadHtml(); 49 | } 50 | 51 | function readTheContent(product){ 52 | const infoProduct = { 53 | image: product.querySelector('div img').src, 54 | title: product.querySelector('.title').textContent, 55 | price: product.querySelector('div p span').textContent, 56 | id: product.querySelector('a').getAttribute('data-id'), 57 | amount: 1 58 | } 59 | 60 | totalCard = parseFloat(totalCard) + parseFloat(infoProduct.price); 61 | totalCard = totalCard.toFixed(2); 62 | 63 | const exist = buyThings.some(product => product.id === infoProduct.id); 64 | if (exist) { 65 | const pro = buyThings.map(product => { 66 | if (product.id === infoProduct.id) { 67 | product.amount++; 68 | return product; 69 | } else { 70 | return product 71 | } 72 | }); 73 | buyThings = [...pro]; 74 | } else { 75 | buyThings = [...buyThings, infoProduct] 76 | countProduct++; 77 | } 78 | loadHtml(); 79 | //console.log(infoProduct); 80 | } 81 | 82 | function loadHtml(){ 83 | clearHtml(); 84 | buyThings.forEach(product => { 85 | const {image, title, price, amount, id} = product; 86 | const row = document.createElement('div'); 87 | row.classList.add('item'); 88 | row.innerHTML = ` 89 | 90 |
91 |
${title}
92 |
${price}$
93 |
Amount: ${amount}
94 |
95 | X 96 | `; 97 | 98 | containerBuyCart.appendChild(row); 99 | 100 | priceTotal.innerHTML = totalCard; 101 | 102 | amountProduct.innerHTML = countProduct; 103 | }); 104 | } 105 | function clearHtml(){ 106 | containerBuyCart.innerHTML = ''; 107 | } -------------------------------------------------------------------------------- /images/cart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garu2/Carrito-de-Compras-JavaScript/7a89950e8a1c40a0e905f13f46a392f9711ea79a/images/cart.png -------------------------------------------------------------------------------- /images/favicon.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /images/logoShop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garu2/Carrito-de-Compras-JavaScript/7a89950e8a1c40a0e905f13f46a392f9711ea79a/images/logoShop.png -------------------------------------------------------------------------------- /images/products/keyboard-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garu2/Carrito-de-Compras-JavaScript/7a89950e8a1c40a0e905f13f46a392f9711ea79a/images/products/keyboard-1.jpg -------------------------------------------------------------------------------- /images/products/keyboard-2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garu2/Carrito-de-Compras-JavaScript/7a89950e8a1c40a0e905f13f46a392f9711ea79a/images/products/keyboard-2.jpg -------------------------------------------------------------------------------- /images/products/keyboard-3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garu2/Carrito-de-Compras-JavaScript/7a89950e8a1c40a0e905f13f46a392f9711ea79a/images/products/keyboard-3.jpg -------------------------------------------------------------------------------- /images/products/keyboard-4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garu2/Carrito-de-Compras-JavaScript/7a89950e8a1c40a0e905f13f46a392f9711ea79a/images/products/keyboard-4.jpg -------------------------------------------------------------------------------- /images/products/keyboard-5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garu2/Carrito-de-Compras-JavaScript/7a89950e8a1c40a0e905f13f46a392f9711ea79a/images/products/keyboard-5.jpg -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Shopping Cart Js 9 | 10 | 11 | 12 | 13 |
14 |
15 | 16 |
17 | 18 |

0

19 |
20 |
21 |

X

22 |

Mi carrito

23 |
24 | 43 |
44 |

Total: $0

45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 | 53 |

20$

54 |
55 |

Tempest Cataclysm Combo 3 En 1 Gaming Teclado

56 | add to cart 57 |
58 |
59 |
60 | 61 |

35$

62 |
63 |

Newskill Suiko Ivory Teclado Mecánico Gaming Full RGB

64 | add to cart 65 |
66 |
67 |
68 | 69 |

15.50$

70 |
71 |

Aukey KM-G16 Teclado Mecánico Gaming Retroiluminado

72 | add to cart 73 |
74 |
75 |
76 | 77 |

20.20$

78 |
79 |

Razer Huntsman Elite Teclado Mecánico Gaming RGB

80 | add to cart 81 |
82 |
83 |
84 | 85 |

19$

86 |
87 |

Trust GXT 856 Torac Teclado Metálico Gaming RGB

88 | add to cart 89 |
90 |
91 |
92 | 93 |

45$

94 |
95 |

MSI Vigor GK30 Teclado Gaming RGB

96 | add to cart 97 |
98 |
99 |
100 | 101 |

23.99$

102 |
103 |

Genesis Thor 300 RGB Teclado Mecánico Gaming RGB Switch Rojo

104 | add to cart 105 |
106 |
107 |
108 | 109 |

50$

110 |
111 |

Mars Gaming MKXTKL Teclado Mecánico Gaming RGB

112 | add to cart 113 |
114 |
115 |
116 | 117 |

16$

118 |
119 |

Trust GXT 881 Odyss Teclado Gaming Semi-Mecánico RGB

120 | add to cart 121 |
122 |
123 |
124 | 125 |

17.50$

126 |
127 |

Krom Krusher Teclado Gaming Híbrido RGB + Ratón

128 | add to cart 129 |
130 |
131 |
132 | 133 |

45$

134 |
135 |

Corsair K55 RGB PRO XT Teclado Gaming Retroiluminado Negro

136 | add to cart 137 |
138 |
139 |
140 | 141 | 144 | 145 | 154 | 155 | 156 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | body{ 2 | font-family: system-ui; 3 | } 4 | header{ 5 | background-color: #e4e4e8; 6 | padding-top: 18px; 7 | padding-bottom: 18px; 8 | box-shadow: 0 7px 8px -3px grey; 9 | position: fixed; 10 | width: 100%; 11 | margin-top: -10px; 12 | z-index: 99; 13 | } 14 | .container{ 15 | max-width: 1200px; 16 | text-align: center; 17 | margin: auto; 18 | } 19 | .header-section{ 20 | display: flex; 21 | justify-content: space-between; 22 | margin: auto; 23 | align-items: center; 24 | position: relative; 25 | } 26 | .header-section div{ 27 | position: relative; 28 | } 29 | .header-section p{ 30 | position: absolute; 31 | top: -24px; 32 | right: -8px; 33 | background-color: #fff; 34 | padding: 1px 6px; 35 | border-radius: 50%; 36 | color: red; 37 | font-weight: 700; 38 | font-family: system-ui; 39 | line-height: 18px; 40 | } 41 | .logo{ 42 | width: 164px; 43 | height: 84px; 44 | cursor: pointer; 45 | } 46 | 47 | .cart{ 48 | width: 39px; 49 | height: 38px; 50 | } 51 | .cart-products{ 52 | position: absolute!important; 53 | top: 82px; 54 | right: -18px; 55 | background-color: #fff; 56 | padding: 0 18px; 57 | box-shadow: 0 3px 10px rgb(0 0 0 / 20%); 58 | display: none; 59 | 60 | min-width: 290px; 61 | } 62 | 63 | .cart:hover{ 64 | margin-bottom: 16px; 65 | transition-duration: .5s; 66 | } 67 | /*end header*/ 68 | .products{ 69 | padding-top: 170px; 70 | display: flex; 71 | flex-wrap: wrap; 72 | justify-content: space-between; 73 | } 74 | 75 | .carts{ 76 | max-width: 250px; 77 | box-shadow: 0 3px 10px rgb(0 0 0 / 20%); 78 | padding-bottom: 34px; 79 | margin-bottom: 40px; 80 | } 81 | .carts img{ 82 | width: 100%; 83 | } 84 | .carts p{ 85 | font-family: system-ui; 86 | font-weight: 500; 87 | padding: 0 20px; 88 | margin-bottom: 24px; 89 | } 90 | .carts div p{ 91 | position: absolute; 92 | top: 0; 93 | right: 0px; 94 | background-color: rgb(0, 235, 129); 95 | color: #fff; 96 | padding: 2px 7px 4px 21px; 97 | clip-path: polygon(25% 0%, 100% 0%, 100% 100%, 25% 100%, 0% 50%); 98 | } 99 | .carts div{ 100 | position: relative; 101 | } 102 | .carts a{ 103 | font-family: system-ui; 104 | text-decoration: none; 105 | background: red; 106 | padding: 9px 23px; 107 | color: #fff; 108 | font-size: 17px; 109 | text-transform: uppercase; 110 | font-weight: 700; 111 | } 112 | 113 | .carts a:hover{ 114 | background: #fff; 115 | color: red; 116 | border: solid 2px red; 117 | transition-duration: .3s; 118 | } 119 | 120 | /*float cart*/ 121 | .cart-products .item img{ 122 | width: 115px; 123 | } 124 | 125 | .cart-products .item{ 126 | display: flex; 127 | align-items: center; 128 | border-bottom: solid 2px red; 129 | } 130 | .cart-products .item .item-content{ 131 | margin: 0 19px; 132 | width: 201px; 133 | } 134 | .cart-products .item .item-content h5{ 135 | margin: 0; 136 | font-size: 15px; 137 | font-weight: 400; 138 | font-family: system-ui; 139 | } 140 | .cart-products .item .item-content h6{ 141 | margin: 0; 142 | font-size: 13px; 143 | font-weight: 400; 144 | font-family: system-ui; 145 | } 146 | .cart-products .item span{ 147 | background-color: red; 148 | padding: 0px 5px; 149 | border-radius: 50%; 150 | color: #fff; 151 | font-family: system-ui; 152 | cursor: pointer; 153 | } 154 | .cart-products h3{ 155 | margin-bottom: 0; 156 | } 157 | .cart-products .item .item-content h5.cart-price{ 158 | font-weight: 700; 159 | } 160 | 161 | /*stye to btn close*/ 162 | p.close-btn{ 163 | top: -4px; 164 | right: 6px; 165 | cursor: pointer; 166 | color: #000; 167 | } 168 | p.author { 169 | display: flex; 170 | justify-content: center; 171 | FONT-VARIANT: JIS04; 172 | margin-top: 3%; 173 | gap: 7px; 174 | } 175 | p.author a { 176 | text-decoration: none; 177 | font-weight: 700; 178 | color: #000; 179 | } --------------------------------------------------------------------------------