您好,登錄后才能下訂單哦!
前言:
我們肯定都很熟悉商品購物車這一功能,每當我們在某寶某東上購買商品的時候,看中了哪件商品,就會加入購物車中,最后結算。購物車這一功能,方便消費者對商品進行管理,可以添加商品,刪除商品,選中購物車中的某一項或幾項商品,最后商品總價也會隨著消費者的操作隨著變化。
包含商品列表,加入購物車按鈕,動態添加商品到購物車
點擊按鈕,實現加入購物車功能,商品移入購物車列表項
點擊左側選擇框,選擇商品,并動態更新總價
點擊上方左側按鈕,實現全選,反選功能,并動態更新價格
點擊右側刪除按鈕,出現提示框,點擊確定,刪除購物車的商品,并動態更新價格
1、用html實現內容
部分關鍵代碼如下,完整代碼見末尾
<div id="car" class="car">
<div class="head_row hid">
<div class="check left"> <i onclick="checkAll()">√</i></div>
<div class="img left"> 全選</div>
<div class="name left">商品名稱</div>
<div class="price left">單價</div>
<div class="number left">數量</div>
<div class="subtotal left">小計</div>
<div class="ctrl left">操作</div>
</div>
</div>
<div id="sum_area">
<div id="pay">去結算</div>
<div id="pay_amout">合計:<span id="price_num">0</span>元</div>
</div>
<div id="box">
<h3 class="box_head"><span>買購物車中商品的人還買了</span></h3>
<ul>
</ul>
</div>
2、用css修飾外觀
3、用JavaScript(jq)設計動態效果。
第一步:首先是進行html頁面的設計,我用一個大的div(id=box)將所有商品包含,商品列表中我用了ul li實現,接著采用克隆的方式,顯示所有商品列表,具體實現代碼如下
<div id="box">
<h3 class="box_head"><span>買購物車中商品的人還買了</span></h3>
<ul>
</ul>
</div>
window.onload = function() {
var aData = [{
"imgUrl": "img/03-car-01.png",
"proName": " 小米手環4 NFC版 ",
"proPrice": "229",
"proComm": "1"
},
{
"imgUrl": "img/03-car-02.png",
"proName": " Redmi AirDots真無線藍牙耳機 ",
"proPrice": "99.9",
"proComm": "9.7"
},
{
"imgUrl": "img/03-car-03.png",
"proName": " 米家藍牙溫濕度計 ",
"proPrice": "65",
"proComm": "1.3"
},
{
"imgUrl": "img/03-car-04.png",
"proName": " 小米小愛智能鬧鐘 ",
"proPrice": "149",
"proComm": "1.1"
},
{
"imgUrl": "img/03-car-05.png",
"proName": "米家電子溫濕度計Pro ",
"proPrice": "750",
"proComm": "0.3"
},
{
"imgUrl": "img/03-car-06.png",
"proName": " 小米手環3 NFC版 Pro ",
"proPrice": "199",
"proComm": "3.3"
},
{
"imgUrl": "img/03-car-07.png",
"proName": " 小米手環3 / 4 通用腕帶",
"proPrice": "19.9",
"proComm": "1.2"
},
{
"imgUrl": "img/03-car-08.png",
"proName": " 米家溫濕度傳感器 ",
"proPrice": "45",
"proComm": "0.6"
},
{
"imgUrl": "img/03-car-09.png",
"proName": " 米家電子溫濕度計Pro 3個裝 ",
"proPrice": "207",
"proComm": "0.3"
},
{
"imgUrl": "img/03-car-10.png",
"proName": " 小米手環3 ",
"proPrice": "199",
"proComm": "7.2"
}
];
var oBox = document.getElementById("box");//商品列表所在的div
var oUl = document.getElementsByTagName("ul")[0];//商品列表所在的ul
//遍歷所有商品
for (var i = 0; i < aData.length; i++) {
var oLi = document.createElement("li");
var data = aData[i];
oLi.innerHTML += '<div class="pro_img"><img src="' + data["imgUrl"] + '" width="150" height="150"></div>';
oLi.innerHTML += '<h4 class="pro_name"><a href="#">' + data["proName"] + '</a></h4>';
oLi.innerHTML += '<p class="pro_price">' + data["proPrice"] + '元</p>';
oLi.innerHTML += '<p class="pro_rank">' + data["proComm"] + '萬人好評</p>';
oLi.innerHTML += '<div class="add_btn">加入購物車</div>';
oUl.appendChild(oLi);
}
第二步:給添加購物車按鈕添加事件
var aBtn = getClass(oBox, "add_btn");//獲取box下的所有添加購物車按鈕
var number = 0;//初始化商品數量
for (var i = 0; i < aBtn.length; i++) {
number++;
aBtn[i].index = i;
aBtn[i].onclick = function() {
var oDiv = document.createElement("div");
var data = aData[this.index];
oDiv.className = "row hid";
oDiv.innerHTML += '<div class="check left"> <i class="i_check" id="i_check" onclick="i_check()" >√</i></div>';
oDiv.innerHTML += '<div class="img left"><img src="' + data["imgUrl"] + '" width="80" height="80"></div>';
oDiv.innerHTML += '<div class="name left"><span>' + data["proName"] + '</span></div>';
oDiv.innerHTML += '<div class="price left"><span>' + data["proPrice"] + '元</span></div>';
oDiv.innerHTML +=' <div class="item_count_i"><div class="num_count"><div class="count_d">-</div><div class="c_num">1</div><div class="count_i">+</div></div> </div>'
oDiv.innerHTML += '<div class="subtotal left"><span>' + data["proPrice"] + '元</span></div>'
oDiv.innerHTML += '<div class="ctrl left"><a href="javascript:;">×</a></div>';
oCar.appendChild(oDiv);
}
}
第三步:給商品數量的增加減少的按鈕添加事件
//獲取所有的數量加號按鈕
var i_btn = document.getElementsByClassName("count_i");
for (var k = 0; k < i_btn.length; k++) {
i_btn[k].onclick = function() {
bt = this;
//獲取小計節點
at = this.parentElement.parentElement.nextElementSibling;
//獲取單價節點
pt = this.parentElement.parentElement.previousElementSibling;
//獲取數量值
node = bt.parentNode.childNodes[1];
console.log(node);
num = node.innerText;
num = parseInt(num);
num++;
node.innerText = num;
//獲取單價
price = pt.innerText;
price = price.substring(0, price.length - 1);
//計算小計值
at.innerText = price * num + "元";
//計算總計值
getAmount();
}
}
//獲取所有的數量減號按鈕
var d_btn = document.getElementsByClassName("count_d");
for (k = 0; k < i_btn.length; k++) {
d_btn[k].onclick = function() {
bt = this;
//獲取小計節點
at = this.parentElement.parentElement.nextElementSibling;
//獲取單價節點
pt = this.parentElement.parentElement.previousElementSibling;
//獲取c_num節點
node = bt.parentNode.childNodes[1];
num = node.innerText;
num = parseInt(num);
if (num > 1) {
num--;
}
node.innerText = num;
//獲取單價
price = pt.innerText;
price = price.substring(0, price.length - 1);
//計算小計值
at.innerText = price * num + "元";
//計算總計值
getAmount();
}
}
第四步:刪除某個商品
//獲取刪除按鈕,并添加事件
var delBtn = oDiv.lastChild.getElementsByTagName("a")[0];
delBtn.onclick = function() {
var result = confirm("確定刪除嗎?");
if (result) {
oCar.removeChild(oDiv);
number--;
getAmount();
}
}
第五步:全選
通過類名的添加與刪除,實現選中和非選中功能
var index = false;
function checkAll() {
var choose = document.getElementById("car").getElementsByTagName("i");
// console.log(choose);
if (choose.length != 1) {
for (i = 1; i < choose.length; i++) {
if (!index) {
choose[0].classList.add("i_acity2")
choose[i].classList.add("i_acity");
} else {
choose[i].classList.remove("i_acity");
choose[0].classList.remove("i_acity2")
}
}
index = !index;
}
getAmount();
}
第六步:單選
選中某個或多個商品,首先判斷選中狀態,然后通過類名的添加與刪除,實現選中和非選中功能,
//獲取選中框
var check = oDiv.firstChild.getElementsByTagName("i")[0];
check.onclick = function() {
// console.log(check.className);
if (check.className == "i_check i_acity") {
check.classList.remove("i_acity");
} else {
check.classList.add("i_acity");
}
getAmount();
}
第七步:動態更新總價
//進行價格合計
function getAmount() {
//獲取選中的選擇框
ns = document.getElementsByClassName("i_acity");
//初始化總價
sum = 0;
//選中框
document.getElementById("price_num").innerText = sum;
for (y = 0; y < ns.length; y++) {
//小計
amount_info = ns[y].parentElement.parentElement.lastElementChild.previousElementSibling;
num = parseInt(amount_info.innerText);
sum += num;
document.getElementById("price_num").innerText = sum;
}
}
注意:圖片地址請自行修改
在線演示地址:點擊進入在線演示
附上下載地址,可以直接運行:點擊進入下載地址
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/help-center.css" />
<title>我的購物車</title>
<style>
* {
margin: 0;
padding: 0;
font-family: "微軟雅黑";
list-style: none;
color: #666;
text-decoration: none;
font-size: 14px;
}
body {
background: #f5f5f5;
height: 100%;
}
.header{
font-size: 12px;
border-bottom: 2px solid #ff6700;
background: #fff;
color: #b0b0b0;
position: relative;
z-index: 20;
height: 100px;
}
.header .container {
position: relative;
width: 1226px;
margin-right: auto;
margin-left: auto;
}
.header .container .header-logo {
width: 93px;
margin-top: 26px;
}
.logo {
width: 48px;
height: 48px;
position: relative;
display: block;
width: 55px;
height: 55px;
overflow: hidden;
background-color: #ff6700;
}
.header-title {
float: left;
margin-top: 26px;
font-size: 12px;
}
.topbar-info {
margin-top: 30px;
line-height: 40px;
}
.link {
padding: 0 5px;
color: #757575;
text-decoration: none;
}
.hid {
overflow: hidden;
}
.left {
float: left;
}
.box_head{
position: relative;
margin: 0;
height: 50px;
font-size: 30px;
font-weight: 400;
color: #757575;
border-top: 1px solid #e0e0e0;
}
.box_head span{
position: absolute;
top: -20px;
left: 372px;
height: 40px;
width: 482px;
line-height: 40px;
text-align: center;
display: block;
background-color: #f5f5f5;
font-size: 30px;
}
#box {
width:1240px;
margin: 20px auto;
}
#box ul {
margin-right: -14px;
overflow: hidden;
}
#box li {
width: 234px;
float: left;
margin-right: 14px;
padding: 24px 0 20px;
background: #FFF;
text-align: center;
position: relative;
cursor: pointer;
margin-bottom:14px;
}
.pro_img {
width: 150px;
height: 150px;
margin: 0 auto 18px;
}
.pro_name {
display: block;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
font-weight: 400;
}
.pro_name a {
color: #333;
}
.pro_price {
color: #ff6700;
margin: 10px;
}
.pro_rank {
color: #757575;
margin: 10px;
}
#box li:hover .add_btn {
display: block;
}
#box li:hover .pro_rank {
opacity: 0;
}
#box li .add_btn:hover {
background-color: #f60;
color: white;
}
.add_btn {
height: 22px;
position: absolute;
width: 122px;
bottom: 28px;
left: 50%;
margin-left: -61px;
line-height: 22px;
display: none;
color: #F60;
font-size: 12px;
border: 1px solid #f60;
}
.car {
width: 1240px;
margin: 20px auto;
background: #FFF;
}
.car .check{
width: 50px;
}
.car .check i{
color: #fff;
display: inline-block;
width: 18px;
height: 18px;
line-height: 18px;
border: 1px solid #e0e0e0;
margin-left: 24px;
background-color: #fff;
font-size: 16px;
text-align: center;
vertical-align: middle;
position: relative;
top: -1px;
cursor: pointer;
font-family: "iconfont";
}
.i_acity {
border-color: #ff6700 !important;
background-color: #ff6700 !important;
}
.car .img {
width: 190px;
}
.car .img img {
display: block;
width: 80px;
height: 80px;
margin: 3px auto;
}
.car .name {
width: 300px;
}
.car .name span {
line-height: 1;
margin-top: 8px;
margin-bottom: 8px;
font-size: 18px;
font-weight: normal;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
.car .price {
width: 144px;
text-align: right;
padding-right: 84px;
}
.car .price span {
color: #ff6700;
font-size: 16px;
}
.car .number{
width: 150px;
}
.car .subtotal{
width: 130px;
}
.car .ctrl {
width: 105px;
padding-right:25px;
text-align: center;
}
.car .ctrl a {
font-size: 20px;
cursor: pointer;
display: block;
width: 26px;
height: 26px;
margin: 30px auto;
line-height: 26px;
}
.car .ctrl a:hover {
color: #FFF;
background: #ff6700;
border-radius: 50%;
}
.head_row {
height: 70px;
line-height: 70px;
}
.head_row, .row {
border-bottom: solid 1px #e0e0e0;
}
.row {
height: 86px;
line-height:86px;
padding: 15px 0;
margin: 0px;
}
#sum_area{
width:1240px;
height: 60px;
background: white;
margin: 20px auto;
}
#sum_area #pay{
width:250px;
height:60px;
text-align: center;
float: right;
line-height: 60px;
font-size: 19px;
background: #FF4B00;
color: white;
}
#sum_area #pay_amout{
width:250px;
height:60px;
text-align: center;
float: right;
line-height: 60px;
font-size: 16px;
color:#FF4B00 ;
}
#sum_area #pay_amout #price_num{
width:100px;
height: 60px;
font-size: 25px;
color:#FF4B00 ;
/* float: left;*/
}
.item_count_i{
height: 85px;
width:10%;
/*border: 1px solid black;*/
float: left;
margin-right: 25px;
}
.num_count{
width:150px;
height:40px;
border: 1.2px solid #E0E0E0;
float:right;
margin-top: 21px;
}
.count_i{
width:30%;
height:40px;
line-height: 40px;
float: left;
text-align: center;
font-size:21px;
color: #747474;
}
.count_i:hover{
width:30%;
height:40px;
line-height: 40px;
float: left;
text-align: center;
font-size:21px;
color: #747474;
background: #E0E0E0;
cursor: pointer;
}
.c_num{
width:40%;
height:40px;
line-height: 40px;
float: left;
text-align: center;
font-size:16px;
color: #747474;
}
.count_d{
width:30%;
height:40px;
line-height: 40px;
float: left;
text-align: center;
font-size:25px;
color: #747474;
}
.count_d:hover{
width:30%;
height:40px;
line-height: 40px;
float: left;
text-align: center;
font-size:25px;
color: #747474;
background: #E0E0E0;
cursor: pointer;
}
.i_acity2 {
border-color: #ff6700 !important;
background-color: #ff6700 !important;
}
</style>
</head>
<body>
<script>
window.onload = function() {
var aData = [{
"imgUrl": "img/03-car-01.png",
"proName": " 小米手環4 NFC版 ",
"proPrice": "229",
"proComm": "1"
},
{
"imgUrl": "img/03-car-02.png",
"proName": " Redmi AirDots真無線藍牙耳機 ",
"proPrice": "99.9",
"proComm": "9.7"
},
{
"imgUrl": "img/03-car-03.png",
"proName": " 米家藍牙溫濕度計 ",
"proPrice": "65",
"proComm": "1.3"
},
{
"imgUrl": "img/03-car-04.png",
"proName": " 小米小愛智能鬧鐘 ",
"proPrice": "149",
"proComm": "1.1"
},
{
"imgUrl": "img/03-car-05.png",
"proName": "米家電子溫濕度計Pro ",
"proPrice": "750",
"proComm": "0.3"
},
{
"imgUrl": "img/03-car-06.png",
"proName": " 小米手環3 NFC版 Pro ",
"proPrice": "199",
"proComm": "3.3"
},
{
"imgUrl": "img/03-car-07.png",
"proName": " 小米手環3 / 4 通用腕帶",
"proPrice": "19.9",
"proComm": "1.2"
},
{
"imgUrl": "img/03-car-08.png",
"proName": " 米家溫濕度傳感器 ",
"proPrice": "45",
"proComm": "0.6"
},
{
"imgUrl": "img/03-car-09.png",
"proName": " 米家電子溫濕度計Pro 3個裝 ",
"proPrice": "207",
"proComm": "0.3"
},
{
"imgUrl": "img/03-car-10.png",
"proName": " 小米手環3 ",
"proPrice": "199",
"proComm": "7.2"
}
];
var oBox = document.getElementById("box");
var oCar = document.getElementById("car");
var oUl = document.getElementsByTagName("ul")[0];
for (var i = 0; i < aData.length; i++) {
var oLi = document.createElement("li");
var data = aData[i];
oLi.innerHTML += '<div class="pro_img"><img src="' + data["imgUrl"] + '" width="150" height="150"></div>';
oLi.innerHTML += '<h4 class="pro_name"><a href="#">' + data["proName"] + '</a></h4>';
oLi.innerHTML += '<p class="pro_price">' + data["proPrice"] + '元</p>';
oLi.innerHTML += '<p class="pro_rank">' + data["proComm"] + '萬人好評</p>';
oLi.innerHTML += '<div class="add_btn">加入購物車</div>';
oUl.appendChild(oLi);
}
var aBtn = getClass(oBox, "add_btn");//獲取box下的所有添加購物車按鈕
var number = 0;//初始化商品數量
for (var i = 0; i < aBtn.length; i++) {
number++;
aBtn[i].index = i;
aBtn[i].onclick = function() {
var oDiv = document.createElement("div");
var data = aData[this.index];
oDiv.className = "row hid";
oDiv.innerHTML += '<div class="check left"> <i class="i_check" id="i_check" onclick="i_check()" >√</i></div>';
oDiv.innerHTML += '<div class="img left"><img src="' + data["imgUrl"] + '" width="80" height="80"></div>';
oDiv.innerHTML += '<div class="name left"><span>' + data["proName"] + '</span></div>';
oDiv.innerHTML += '<div class="price left"><span>' + data["proPrice"] + '元</span></div>';
oDiv.innerHTML +=' <div class="item_count_i"><div class="num_count"><div class="count_d">-</div><div class="c_num">1</div><div class="count_i">+</div></div> </div>'
oDiv.innerHTML += '<div class="subtotal left"><span>' + data["proPrice"] + '元</span></div>'
oDiv.innerHTML += '<div class="ctrl left"><a href="javascript:;">×</a></div>';
oCar.appendChild(oDiv);
var flag = true;
var check = oDiv.firstChild.getElementsByTagName("i")[0];
check.onclick = function() {
// console.log(check.className);
if (check.className == "i_check i_acity") {
check.classList.remove("i_acity");
} else {
check.classList.add("i_acity");
}
getAmount();
}
var delBtn = oDiv.lastChild.getElementsByTagName("a")[0];
delBtn.onclick = function() {
var result = confirm("確定刪除嗎?");
if (result) {
oCar.removeChild(oDiv);
number--;
getAmount();
}
}
var i_btn = document.getElementsByClassName("count_i");
for (var k = 0; k < i_btn.length; k++) {
i_btn[k].onclick = function() {
bt = this;
//獲取小計節點
at = this.parentElement.parentElement.nextElementSibling;
//獲取單價節點
pt = this.parentElement.parentElement.previousElementSibling;
//獲取數量值
node = bt.parentNode.childNodes[1];
console.log(node);
num = node.innerText;
num = parseInt(num);
num++;
node.innerText = num;
//獲取單價
price = pt.innerText;
price = price.substring(0, price.length - 1);
//計算小計值
at.innerText = price * num + "元";
//計算總計值
getAmount();
}
}
//獲取所有的數量減號按鈕
var d_btn = document.getElementsByClassName("count_d");
for (k = 0; k < i_btn.length; k++) {
d_btn[k].onclick = function() {
bt = this;
//獲取小計節點
at = this.parentElement.parentElement.nextElementSibling;
//獲取單價節點
pt = this.parentElement.parentElement.previousElementSibling;
//獲取c_num節點
node = bt.parentNode.childNodes[1];
num = node.innerText;
num = parseInt(num);
if (num > 1) {
num--;
}
node.innerText = num;
//獲取單價
price = pt.innerText;
price = price.substring(0, price.length - 1);
//計算小計值
at.innerText = price * num + "元";
//計算總計值
getAmount();
}
}
delBtn.onclick = function() {
var result = confirm("確定刪除嗎?");
if (result) {
oCar.removeChild(oDiv);
number--;
getAmount();
}
}
}
}
}
function getClass(oBox, tagname) {
var aTag = oBox.getElementsByTagName("*");
var aBox = [];
for (var i = 0; i < aTag.length; i++) {
if (aTag[i].className == tagname) {
aBox.push(aTag[i]);
}
}
return aBox;
}
var index = false;
function checkAll() {
var choose = document.getElementById("car").getElementsByTagName("i");
// console.log(choose);
if (choose.length != 1) {
for (i = 1; i < choose.length; i++) {
if (!index) {
choose[0].classList.add("i_acity2")
choose[i].classList.add("i_acity");
} else {
choose[i].classList.remove("i_acity");
choose[0].classList.remove("i_acity2")
}
}
index = !index;
}
getAmount();
}
//進行價格合計
function getAmount() {
// console.log(ys);
ns = document.getElementsByClassName("i_acity");
console.log(ns);
sum = 0;
//選中框
document.getElementById("price_num").innerText = sum;
for (y = 0; y < ns.length; y++) {
//小計
amount_info = ns[y].parentElement.parentElement.lastElementChild.previousElementSibling;
num = parseInt(amount_info.innerText);
sum += num;
document.getElementById("price_num").innerText = sum;
}
}
</script>
</head>
<body>
<div class="header">
<div class="container">
<div class="header-logo">
<a class="logo ir" href="" title="小米官網">小米官網</a>
</div>
<div class="header-title" id="J_miniHeaderTitle">
<h3 >我的購物車</h3>
</div>
<div class="topbar-info" id="J_userInfo">
<a class="link" >登錄</a><span class="sep">|</span><a class="link" >注冊</a></div>
</div>
</div>
<div id="car" class="car">
<div class="head_row hid">
<div class="check left"> <i onclick="checkAll()">√</i></div>
<div class="img left"> 全選</div>
<div class="name left">商品名稱</div>
<div class="price left">單價</div>
<div class="number left">數量</div>
<div class="subtotal left">小計</div>
<div class="ctrl left">操作</div>
</div>
</div>
<div id="sum_area">
<div id="pay">去結算</div>
<div id="pay_amout">合計:<span id="price_num">0</span>元</div>
</div>
<div id="box">
<h3 class="box_head"><span>買購物車中商品的人還買了</span></h3>
<ul>
</ul>
</div>
</body>
</html>
感謝您的閱讀,有什么問題或者意見歡迎您提出
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。