There are a lot of codes for shopping cart implementation on the Internet. This article mainly shares with you the HTML code to implement a simple shopping cart. Friends who need it can take a look. Next, I will explain the specific details. accomplish.
1. Use HTML to implement content;
2. Use CSS to modify the appearance;
#3. Use js (jq) to design dynamic effects.
The first step: first is to design the html page, I use a large p contains all products, and then uses different p to package different products. I used ul li to implement the product list. The specific implementation code is as follows (the products involved in the code are all copied casually online and have no reference value. ):
<p id="goods"> <p class="goodsItem"> <ul class="goditem"> <li class="godpic"><img src="/static/imghwm/default1.png" data-src="images/1.png" class="lazy" alt="HTML code to implement a simple shopping cart" ></li> <li class="godprice">¥25.00</li> <li class="godinfo">《飞鸟集》中很多诗歌是用孟加拉文创作的,这部诗集最早由郑振铎先生译介到中国。</li> <li class="godadd"><a href="javascript:;">加入购物车</a></li> </ul> </p> <p class="goodsItem"> <ul class="goditem"> <li class="godpic"><img src="/static/imghwm/default1.png" data-src="images/2.png" class="lazy" alt="HTML code to implement a simple shopping cart" ></li> <li class="godprice">¥56.00</li> <li class="godinfo">本书主要介绍了如何使用现有的Web 相关技术构建Android 应用程序。</li> <li class="godadd"><a href="javascript:;">加入购物车</a></li> </ul> </p> <p class="goodsItem"> <ul class="goditem"> <li class="godpic"><img src="/static/imghwm/default1.png" data-src="images/3.png" class="lazy" alt="HTML code to implement a simple shopping cart" ></li> <li class="godprice">¥37.00</li> <li class="godinfo">用文字打败时间。冯唐最畅销作品,杂文才是其销量最好、最受欢迎的作品。</li> <li class="godadd"><a href="javascript:;">加入购物车</a></li> </ul> </p> <p class="goodsItem"> <ul class="goditem"> <li class="godpic"><img src="/static/imghwm/default1.png" data-src="images/1.png" class="lazy" alt="HTML code to implement a simple shopping cart" ></li> <li class="godprice">¥25.00</li> <li class="godinfo">《飞鸟集》中很多诗歌是用孟加拉文创作的,这部诗集最早由郑振铎先生译介到中国。</li> <li class="godadd"><a href="javascript:;">加入购物车</a></li> </ul> </p> <p class="goodsItem"> <ul class="goditem"> <li class="godpic"><img src="/static/imghwm/default1.png" data-src="images/2.png" class="lazy" alt="HTML code to implement a simple shopping cart" ></li> <li class="godprice">¥56</li> <li class="godinfo">本书主要介绍了如何使用现有的Web 相关技术构建Android 应用程序。</li> <li class="godadd"><a href="javascript:;">加入购物车</a></li> </ul> </p> <p class="goodsItem"> <ul class="goditem"> <li class="godpic"><img src="/static/imghwm/default1.png" data-src="images/3.png" class="lazy" alt="HTML code to implement a simple shopping cart" ></li> <li class="godprice">¥37.00</li> <li class="godinfo">用文字打败时间。冯唐最畅销作品,杂文才是其销量最好、最受欢迎的作品。</li> <li class="godadd"><a href="javascript:;">加入购物车</a></li> </ul> </p> </p> <p id="godcar"> <p class="dnum">0</p> <p class="dcar"> <img src="/static/imghwm/default1.png" data-src="images/car.jpg" class="lazy" alt="HTML code to implement a simple shopping cart" > </p> </p>
which involves a knowledge point:
<li class="godadd"><a href="javascript:;">加入购物车</a></li>
, I used javascript:; This means no jump, execute an empty event.
Step 2: Design , for better display, I will include the p of each product list After setting the width, height, and border, it is worth noting that in order to fix the shopping cart in a certain position, I set its position to fixed, and then set top and left to fix it at the position you want. In addition, you must learn to use margin and padding flexibly to make the display more beautiful.
Note: If you want to set width and height or other blocks for inline elements Level element attributes, then you need to set display:block.
The specific design code is as follows:
* { padding: 0px; margin: 0px; font-family: "微软雅黑"; } .goodsItem{ width:280px; height: 400px; float: left; border: 1px solid #ccc; margin:5px; } #goods{ width:910px; } .goditem{ list-style: none; } .godpic img{ display: block; width:250px; height: 250px; margin:0px auto; } .godprice,.godinfo,.godadd{ display: block; width:220px; margin:0px auto; text-align: center; } .godprice{ font-size: 20px; color: #f00; } .godinfo{ text-align: center; font-size: 14px; margin: 10px 0px; } .godadd a{ display: block; width: 150px; height: 36px; background-color: #fd6a01; border-radius: 10px; margin: 0px auto; text-decoration: none; color:#fff; line-height: 36px; } #godcar{ position: fixed; right: 0px; top:40%; width: 72px; height: 64px; } #godcar .dnum{ width:24px; height: 24px; border-radius: 12px; background-color: #f00; text-align: center; line-height: 24px; position: absolute; font-size: 12px; top:0px; } .godadd .bg { background-color: #808080; }
The first * means setting attributes for all elements. It's a good practice to set margin and padding at the beginning.
Step 3: After realizing the static page, you need to implement the specific shopping cart through jq, such as adding Shopping cart, shopping cart quantity changes, etc. I spent some time designing: how to make the image slowly move to the shopping cart, then become smaller, and finally disappear when the product is added to the shopping cart. Among them, I used the animate function to implement this process. The difficulty in realizing this function is: how to move and change the picture. Next, we will explain how to implement this process:
1) First, you need to obtain the picture of the product, and then copy the obtained picture;
var img = $(this).parent().find(".godpic").find("img"); var cimg = img.clone();
2) Get the top and left values of the product image and the top and left values of the shopping cart, so that it can be achieved through the animate function Move;
var imgtop = img.offset().top; var imgleft = img.offset().left; var cartop = $("#godcar").offset().top; var carleft = $("#godcar").offset().left;
3) Write the animate function to achieve specific effects;
cimg.appendTo($("body")).css({ "position": "absolute",//绝对定位 "opacity": "0.7", "top": imgtop, "left": imgleft }).animate({ "top": cartop, "left": carleft, "width": "40px", "height": "40px", "opacity": "0.3" //透明度 }, 1000, function () { cimg.remove(); //图片消失 $(".dnum").text(i); //购物车数量变化 });
Simple moves and changes It was realized.
## But then I thought, it seems inconsistent with the fact that the quantity of the shopping cart will return to 0 every time I refresh the shopping cart, so I thought about how to prevent the shopping cart from being reset when the page is refreshed. The quantity changed, I checked the information, and summarized three methods:(1) Save to the database;
(2) Through cookie method;
(3)通过h5的localStorage方法; 最后我决定采用第三种方法,因为想试试h5的新方法(出于好奇心理~~,也是因为刚好看到这个方法,就试试看),localStorage 方法存储的数据没有时间限制。第二天、第二周或下一年之后,数据依然可用。我的代码具体实现:localStorage.getItem。 好了,所有该讲的都讲完了,附上jq的所有代码,喜欢的就点个赞: 最终效果图: 聪明的你学会了吗,赶快实践起来吧!
var i = 0;
$(function(){
var inum = 0;
if(localStorage.getItem("inum")!==null){
inum = localStorage.getItem("inum");
}
$(".dnum").text(inum);
$(".godadd").click(function(){
if (!$(this).find("a").hasClass("bg")) {
i++;
$(this).find("a").addClass("bg");
var img = $(this).parent().find(".godpic").find("img");
var cimg = img.clone();
var imgtop = img.offset().top;
var imgleft = img.offset().left;
var cartop = $("#godcar").offset().top;
var carleft = $("#godcar").offset().left;
cimg.appendTo($("body")).css({
"position": "absolute",
"opacity": "0.7",
"top": imgtop,
"left": imgleft
}).animate({
"top": cartop,
"left": carleft,
"width": "40px",
"height": "40px",
"opacity": "0.3"
}, 1000, function () {
cimg.remove();
$(".dnum").text(i);
localStorage.setItem("inum", i);
});
}
});
});
The above is the detailed content of HTML code to implement a simple shopping cart. For more information, please follow other related articles on the PHP Chinese website!

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
