效果:
在商品选购列表中选中的商品可以在购物车页面中同步看到,购物车有添加增减商品数量和删除商品功能
代码:
商品列表页:list.html
html:
<h1>这是商品列表,去<a href="cart.html">购物车</a></h1>
<div id="cont">
<p>卖完了,等待上架中...</p>
</div>
css:
#cont {width: 1000px;border: solid;overflow: hidden;margin: 0 auto;}
#cont .box {width: 250px;text-align: center;border: solid;box-sizing: border-box;float: left;}
.box img {width: 200px;}
.box p {margin: 10px 0;}
.box p:nth-child(3) {height: 40px;line-height: 20px;overflow: hidden;}
.box span {width: 100px;height: 40px;padding: 0 10px;margin: 10px auto;background: cyan;line-height: 40px;text-align: center;display: block;cursor: pointer;}
.box span:active {background: blue;color: #fff;}
js:
引入 good.js文件
class List{
constructor(){
this.cont=document.getElementByid("cont");
this.load();
this.addEvent();
}
load(){
ajax().then(data=>{
this.data=JSON.parse(data);
this.render();
})
}
render(){
let str='';
this.data.forEach(val=>{
str+=`<div class="box" index="${val.proId}">
<img src="${val.proImg}" alt="">
<p>${val.price}</p>
<p>${val.proName}</p>
<span class="btn">加入购物车</span>
</div>`;
});
this.cont.innerHTML=str;
}
addEvent(){
const that=this;
this.cont.addEventListener("click",function(eve){
if(eve.target.className=="btn"){
that.id=eve.target.parentNode.getAttibute("index");
that.setData();
}
});
}
setData(){
const arr=localStorage.getItem("proData")?JSON.parse(localStorage.getItem("proData")):[];
if(arr.length>0){
let i=0;
const onOff=arr.some((val,idx)=>{
i=idx;
return val.id=this.id;
});
if(onOff){
arr[i].num++;
}
else{
arr.push({
id:this.id,
num:1
});
}
}
else{
arr.push({
id:this.id,
num:1
});
}
localStorage.setItem("proData",JSON.stringify(arr);
}
}
new List;
function ajax(){
return new Promise(resolve=>{
setTImeout(()=>{
resolve(JSON.stringify(data));
},500);
});
}
购物车页面:cart.html
html:
<h1>这是购物车页面,<a href="list.html">继续购物</a></h1>
<table border="1" width="800" align="center">
<thead>
<tr>
<th>图片</th>
<th>名称</th>
<th>单价</th>
<th>数量</th>
<th>总价</th>
<th width=40>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="6">购物车为空,<a href="./list.html">继续选购</a></td>
</tr>
</tbody>
</table>
css:
img {width: 100px;}
js:
class Cart{
constructor(){
this tbody=document.getElementById("tbody");
this.load();
this.addEvent();
}
load(){
ajax({
url:"dfhsla"
}).then(data=>{
this.data=JSON.parse(data);
this.render();
})
}
render(){
this.localData=localStorage.getItem("proData")?JSON.parse(localStorage.getItem("proData")):[];
if(this.localData.length<1) return;
let str="";
this.data.forEach(val1=>{
this.localData.forEach(val2=>{
if(val1.proId===val2.id){
str+=`<tr index="${val2.id}>
<td><img src="${val1.proImg}" alt=""></td>
<td>${val1.proName}</td>
<td>${val1.price}</td>
<td><input type="number" min="1" value="${val2.num}"></td>
<td>${val1.price*val2.num}</td>
<td>删除</td>
</tr>`;
}
})
});
this.tbody.innerHTML=str;
}
addEvent(){
const that=this;
this.tbody.addEventListener("click",function(e){
if(e.target.className==="del"){
that.id=e.target.parentNode.getAttribute("input");
e.target.parentNode.remove();
that.setData(i=>that.localData.splice(i,1));
}
})
this.tbody.addEventListener("input",function(e){
if(e.target.className==="set"){
that.id=e.target.parentNode.parentNode.getAttribute("index");
that.setData(i=>that.localData[i].num=e.target.value);
}
})
}
setData(cb){
let i=0;
this.localData.some((val,idx)=>{
i=idx;
return val.id=this.id;
})
cb(i);
localStorage.setItem("proData",JSON.stringify(this.localData));
}
}
new Cart;
function ajax(){
return new Promise((resolve)=>{
setTimeout(function(){
resolve(JSON.stringify(data));
},500)
});
}
商品数据:good.js
var data=[
{
"proId": "proId-50990677-a288-11eb-af86-375d9076f459",
"proImg": "https://img12.360buyimg.com/n1/s450x450_jfs/t1/128286/7/2247/303790/5ec37cabEf2a27dc3/4dec5022f102a39b.jpg",
"proName": "戴尔DELL灵越5000 14英寸酷睿i5轻薄笔记本电脑(十代i5-1035G1 8G 512G MX230 2G独显)银",
"brand": "DELL",
"logo": "http://img30.360buyimg.com/popshop/jfs/t1114/95/265737457/7715/95b70982/550fb619Nc0269ee4.jpg",
"type": "电脑",
"price": 4299,
"sales": 10,
"stock": 155,
"score": 4.8,
"discount": 8,
"describe": "商品名称:戴尔灵越5000商品编号:100006546527商品毛重:2.5kg商品产地:中国大陆显卡类别:入门级游戏独立显卡优选服务:两年质保厚度:15.1mm—18.0mm特性:PCIE高速固态硬盘屏幕尺寸:14-14.9英寸系列:戴尔 - Inspiron显存容量:2GB分类:轻薄笔记本裸机重量:1.5-2kg处理器:Intel i5屏幕刷新率:其他待机时长:5-7小时内存容量:8G显卡型号:MX230硬盘容量:512G SSD系统:Windows 10颜色:银色",
"time": 1618999191639
},
{
}
];