By default, the shopping cart is hidden invisible, when the user clicks Add to the Shopping cart button, the product information will be added to the shopping cart, the cart will appear as a button in the lower right corner of the page, click the button will expand the shopping cart, display shopping cart in the merchandise information, At the same time can also be in the shopping cart items to delete or clearing and other operations. Users can also temporarily close the shopping cart and continue shopping.
HTML structure
The HTML structure mainly consists of two parts, the first part is the "Add to Cart" button in the Product list, the following code, we use the Data-* property to take the product ID, picture, name, price and other information together.
The code is as follows |
Copy Code |
<a href= "#0" class= "btn btn-success Add-button" data-price= "3669.00" data-proid= "1" data-proname= "Huawei P9" data-proimg = "Img/huawei_p9.jpg" > Add to Cart </a>
|
The second part is the shopping cart, the shopping cart part includes the trigger shopping cart as well as the shopping Cart statistics section. Cd-cart-trigger and shopping Cart main content section. Cd-cart.
The code is as follows |
Copy Code |
<div class= "Cd-cart-container Empty" > <a href= "#0" class= "Cd-cart-trigger" > Shopping Cart <ul class= "Count" > <!--cart items Count--> <li>0</li> <li>0</li> </ul> </a>
<div class= "Cd-cart" > <div class= "wrapper" > <span class= "undo" > Deleted <a href= "#0" > Recovery </a></span>
<div class= "Body" > <ul> <!--This part is the shopping cart merchandise part, dynamically inserts--> by JavaScript </ul> </div>
<footer> <a href= "#0" class= "Checkout" ><em> settlement-¥<span>0</span></em></a> </footer> </div> </div> </div> |
The UL list in the Div.body element is empty by default, it is used to display the shopping cart's product list information, its approximate structure is as follows, it is dynamically inserted by JavaScript.
The code is as follows |
Copy Code |
<div class= "Body" > <ul> <li class= "Product" > <div class= "Product-image" > <a href= "#0" ></a> </div>
<div class= "Product-details" >
<span class= "Price" >¥3999.99</span>
<div class= "Actions" > <a href= "#0" class= "Delete-item" > Delete </a>
<div class= "Quantity" > <label for= "cd-product-' + productId + '" > number </label> <span class= "Select" > <span class= "Select" >x<i id= "cd-product-' +proid+ '" >1</i></span> </span> </div> </div> </div> </li>
</ul> </div> |
The CSS section is not shown in this article, you can download the source of the Css/style.css view.
Javascript
This example code is based on jquery, so the jquery library file needs to be loaded ahead of time.
When the user clicks the button. Add-button, the triggering function addproduct (), inserts the product information into. BODY > ul.
The code is as follows |
Copy Code |
function Addproduct (proname,proid,price,proimg) { var quantity = $ ("#cd-product-" +proid). Text (); var select= ', productadded= ';
if (quantity== ') { var select = ' <span class= ' select ' >x<i id= ' cd-product-' +proid+ ' ' >1</i></span> '; var productadded = $ (' <li class= "product" ><div class= "product-image" ><a href= "#0" ></a></div><div class=" product-details ">Cartlist.prepend (productadded); }else{ Quantity = parseint (quantity); $ ("#cd-product-" +proid). HTML (quantity+1); } } |
Actions in a shopping cart, such as deleting a product, restoring a product, and changing the number of items will result in a change in the total amount of settlement, so that the related changes are triggered in the function Updatecartcount () and Updatecarttotal (). OK, the specific JavaScript code please download the source view js/main.js.