html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="header">页面顶部</div>
<div class="content">
</div>
<div class="footer">页面底部</div>
<script src="./index.js"></script>
</body>
</html>
css
* {
margin: 0;
padding: 0;
}
ul,
ol,
li {
list-style: none;
}
.header,
.footer {
width: 1200px;
height: 100px;
background-color: skyblue;
color: #fff;
font-size: 50px;
display: flex;
justify-content: center;
align-items: center;
margin: 0 auto;
}
.footer {
height: 400px;
}
.content {
width: 1200px;
margin: 0 auto;
padding: 10px 0;
}
.content > .top,
.content > .bottom {
height: 50px;
background-color: pink;
display: flex;
align-items: center;
}
.content > .bottom {
justify-content: space-between;
box-sizing: border-box;
padding: 0 10px;
}
.content > .bottom > .totalPrice > span {
font-size: 20px;
color: red;
}
.content > .bottom > .btns > button {
font-size: 18px;
padding: 5px 10px;
cursor: pointer;
}
.content > .top > input {
width: 30px;
height: 30px;
margin: 0 15px 0 50px;
}
.content > ul {
padding-top: 10px;
}
.content > ul > li {
width: 100%;
border: 1px solid #333;
box-sizing: border-box;
height: 100px;
margin-bottom: 10px;
display: flex;
}
.content > ul > li > div {
display: flex;
justify-content: center;
align-items: center;
border-right: 1px solid #333;
}
.content > ul > li > div:last-child {
border: none;
}
.content > ul > li > .show,
.content > ul > li > .status {
width: 100px;
}
.content > ul > li > .status > input {
width: 30px;
height: 30px;
}
.content > ul > li > .show > img {
width: 100%;
height: 100%;
display: block;
}
.content > ul > li > .price,
.content > ul > li > .sub {
width: 200px;
color: red;
font-size: 20px;
}
.content > ul > li > .title {
width: 300px;
align-items: flex-start;
justify-content: flex-start;
box-sizing: border-box;
padding: 5px;
}
.content > ul > li > .number {
width: 230px;
}
.content > ul > li > .number > input {
width: 50px;
height: 30px;
text-align: center;
margin: 0 5px;
border: none;
outline: none;
font-size: 18px;
}
.content > ul > li > .number > button {
width: 30px;
height: 30px;
cursor: pointer;
}
.content > ul > li > .destory {
flex: 1;
}
.content > ul > li > .destory > button {
padding: 5px;
font-size: 18px;
cursor: pointer;
}
JS
var cartList = JSON.parse(window.localStorage.getItem("cartList")) || [
{
id: 111234,
status: true,
pic: "https://img1.baidu.com/it/u=2511310783,721605137&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=332",
name: "我是一个手机, 不知道是啥",
price: 100,
number: 3,
total: 16,
},
{
id: 123456,
status: true,
pic: "https://img1.baidu.com/it/u=1537709578,2453227648&fm=253&fmt=auto&app=120&f=JPEG?w=809&h=500",
name: "我是一个电脑, 不知道是啥",
price: 98.72,
number: 1,
total: 3,
},
{
id: 965874,
status: false,
pic: "https://img2.baidu.com/it/u=3561506717,735421650&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500",
name: "我是一个手纸, 不知道是啥",
price: 356.21,
number: 2,
total: 22,
},
];
var content = document.querySelector(".content");
function myFn() {
var selectItem = 0;
var selectItemNum = 0;
var totalNum = 0;
cartList.forEach(function (item) {
if (item.status) {
selectItem++;
selectItemNum += item.number;
totalNum += item.price * item.number;
}
});
var strHtml = `
<div class="top">
<input type="checkbox" class="select_all" ${
selectItem === cartList.length && cartList.length !== 0
? "checked"
: ""
}> 全选
</div>
<ul>`;
strHtml += cartList.reduce(function (prev, item) {
return (
prev +
`<li>
<div class="status">
<input data-id="${
item.id
}" class="status_item" type="checkbox" ${
item.status ? "checked" : ""
}>
</div>
<div class="show">
<img src="${item.pic}" alt="">
</div>
<div class="title">
${item.name}
</div>
<div class="price">
¥ ${item.price}
</div>
<div class="number">
<button class="subs" data-id="${item.id}">-</button>
<input type="text" value="${item.number}">
<button class="add" data-id="${item.id}">+</button>
</div>
<div class="sub">
¥ ${(item.price * item.number).toFixed(2)}
</div>
<div class="destory">
<button data-id="${item.id}" class="del">删除</button>
</div>
</li>`
);
}, "");
strHtml += `</ul>
<div class="bottom">
<div class="totalNum">
总件数 : ${selectItemNum}
</div>
<div class="btns">
<button class="clear">清空购物车</button>
<button class="go_pay" data-price="${totalNum}">去结算</button>
<button class="del_item">删除所有已选中</button>
</div>
<div class="totalPrice">
总价格 : ¥ <span>${totalNum.toFixed(2)}</span>
</div>
</div>`;
content.innerHTML = strHtml;
window.localStorage.setItem("cartList", JSON.stringify(cartList));
}
myFn();
content.onclick = function (e) {
if (e.target.className === "select_all") {
cartList.forEach(function (item) {
item.status = e.target.checked;
});
myFn();
}
if (e.target.className === "clear") {
if (!confirm("请问您是否删除")) return;
cartList = [];
myFn();
}
if (e.target.className === "go_pay") {
console.log(e.target.dataset.price);
}
if (e.target.className === "del_item") {
if (!confirm("请问您是否删除")) return;
cartList = cartList.filter(function (item) {
return !item.status;
});
myFn();
}
if (e.target.className === "subs") {
var i = cartList.find(function (item) {
return item.id === e.target.dataset.id - 0;
});
if (i.number === 1) return alert("商品收藏数量最小为 1");
i.number--;
myFn();
}
if (e.target.className === "add") {
var i = cartList.find(function (item) {
return item.id === e.target.dataset.id - 0;
});
if (i.number === i.total) return alert("商品收藏数量不能超过库存");
i.number++;
myFn();
}
if (e.target.className === "status_item") {
var i = cartList.find(function (item) {
return item.id === e.target.dataset.id - 0;
});
i.status = !i.status;
myFn();
}
if (e.target.className === "del") {
if (!confirm("请问您是否删除")) return;
cartList = cartList.filter(function (item) {
return item.id !== e.target.dataset.id - 0;
});
myFn();
}
};