Based on JavaScript implementation add to cart effect attach source download _javascript tips

Source: Internet
Author: User
Tags jquery library

There are a number of ways to add a product to a shopping cart, usually by clicking the "Add to Cart" button, jumping to the shopping cart, and clicking the "Checkout" button in the shopping cart to settle. And today I'm going to introduce you to a more friendly solution.

View Demo Download source code

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.

<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.

<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" > 
 
 

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.

<div class= "Body" > 
<ul> 
<li class= "Product" > 
<div class= "Product-image" > 
<a href= "#0" ></a> 
</div> 
<div class= " Product-details "> 
 
 

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, triggers the function addproduct (), inserts the product information into. BODY > ul.

 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 "> 
 

The above is a small series to introduce the implementation of the Java based on JavaScript to add to the effect of the shopping cart to download the source, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.