goodsList部分
购物车菜单html样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./css/reset.css">
<link rel="stylesheet" href="./css/goodsList.css">
</head>
<body>
<div class="wrap">
<div class="header">
<h1>商品列表</h1>
<a href="./goodsCart.html">我的购物车</a>
</div>
<div class="nav"> 首页 》 鞋子 》 男鞋 》 帆布鞋
</div>
<div class="main">
<div class="content">
</div>
</div>
</div>
<script src="./js/jquery-1.8.3.js"></script>
<script src="./js/goodsList.js"></script></body>
</html>
购物车菜单css样式
.wrap{
width: 1000px;
margin: 0 auto;
}
.header{
background-color: #eee;
position: relative;
}
.header h1{
text-align: center;
line-height: 80px;
font-size: 30px;
}
.header a{
position: absolute;
right: 10px;
top: 10px;
color: red;
padding: 4px 8px;
border: 1px solid red;
}
.nav{
padding: 15px 0;
}
.main{
overflow: hidden;
}
.content{
width: 1050px;
overflow: hidden;
}
.goods{
float: left;
width: 235px;
margin-right: 20px;
margin-bottom: 30px;
border: 1px solid #fff;
}
.goods:hover{
border-color: red;
}
.goods img{
width: 235px;
}
.goods p{
color: #f40;
font-size: 18px;
padding: 6px 0;
}
.goods div{
border: 1px solid #f40;
color: #f40;
font-size: 14px;
padding-left: 40px;
line-height: 30px;
background: url(../img/cart.png) 5px 4px no-repeat;
float: right;
padding-right: 6px;
cursor: pointer;
}
购物车菜单js部分
$(function (){
$.ajax({
url: './data/goods.json',
type: 'get', dataType: 'json',
success: function (json){
var goodsStr = ''
$.each(json,function (index,item){
goodsStr += `<div class="goods">
<img src="${item.imgurl}" alt="">
<p>${item.price}</p>
<h3>${item.title}</h3>
<div code="${item.code}">加入购物车</div>
</div>`
})
$('.content').html(goodsStr)
}
})
$('.content').on('click','.goods div',function (){
var code = $(this).attr('code')
if (localStorage.getItem('goods')) {
var goodsArr = JSON.parse( localStorage.getItem('goods') )
} else {
var goodsArr = []
}
var hasGoods = false
if (goodsArr.length > 0) {
$.each(goodsArr,function (index,item){
console.log(index)
console.log(item)
if (item.code === code) {
item.num++
hasGoods = true
return false
}
})
}
if (!hasGoods) {
goodsArr.push({code:code,num:1})
}
localStorage.setItem('goods',JSON.stringify(goodsArr))
alert('添加购物车成功')
})
})
goodsCart部分
购物车主要页面html部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./css/reset.css">
<link rel="stylesheet" href="./css/goodsCart.css">
</head>
<body>
<div class="wrap">
<div class="header">
<h1>我的购物车</h1>
</div>
<div class="main">
<div class="title">
<span class="a1">商品</span>
<span class="a2">单价</span>
<span class="a3">数量</span>
<span class="a4">操作</span>
</div>
<ul class="list">
</ul>
</div>
</div>
<script src="./js/jquery-1.8.3.js"></script>
<script src="./js/goodsCart.js"></script>
</body>
</html>
购物车主要页面css部分
.wrap{
width: 1000px;
margin: 0 auto;
}
.header{
background-color: #eee;
}
.header h1{
line-height: 80px;
text-align: center;
font-size: 30px;
}
.title{
line-height: 40px;
background-color: #ccc;
overflow: hidden;
margin-top: 30px;
}
.title span{
font-size: 14px;
color: #333;
}
.a1{
margin-left: 100px;
}
.a2{
margin-left: 450px;
}
.a3{
margin-left: 100px;
}
.a4{
margin-left: 100px;
}
.list li{
margin: 10px 0;
overflow: hidden;
}
.list li img{
width: 80px;
height: 80px;
float: left;
}
.list li h3{
float: left;
margin-left: 10px;
width: 340px;
padding: 10px 0;
}
.list li p{
float: left;
margin-left: 120px;
width: 100px;
padding: 10px 0;
text-align: center;
}
.list li span{
float: left;
margin-left: 30px;
width: 100px;
padding: 10px 0;
text-align: center;
}
.list li em{
float: left;
margin-left: 30px;
width: 100px;
padding: 10px 0;
text-align: center;
}
购物车主要页面js部分
$(function (){
if (localStorage.getItem('goods')) {
var goodsArr = JSON.parse( localStorage.getItem('goods') )
$.ajax({
url: './data/goods.json',
type: 'get',
dataType: 'json',
success: function (json){
var domStr = ''
$.each(goodsArr,function (index,item){
$.each(json,function (ind,obj){
if ( item.code === obj.code ) {
domStr += `
<li>
<img src="${obj.imgurl}" alt="">
<h3>${obj.title}</h3>
<p>${obj.price}</p>
<span>${item.num}</span>
<em code="${obj.code}">删除</em>
</li>
`
}
})
})
$('.list').html(domStr)
}
})
$('.list').on('click','li em',function (){
$(this).parent().remove()
var code = $(this).attr('code')
$.each(goodsArr,function (index,item){
if (item.code === code) {
goodsArr.splice(index,1)
return false
}
})
if (goodsArr.length > 0) {
localStorage.setItem('goods',JSON.stringify(goodsArr))
} else {
localStorage.removeItem('goods')
var nodata = '<li style="line-height: 70px; text-align: center;">购物车暂无数据!</li>'
$('.list').html(nodata)
}
alert('商品移出购物车成功!')
})
} else {
var nodata = '<li style="line-height: 70px; text-align: center;">购物车暂无数据!</li>'
$('.list').html(nodata)
}
})
通用css
@charset "utf-8";
html,
body,
ul,
li,
ol,
dl,
dd,
dt,
p,
h1,
h2,
h3,
h4,
h5,
h6,
form,
fieldset,
legend,
img,
input,
figure,
figcaption
{
margin: 0;
padding: 0;
box-sizing:
border-box;
}
header,footer,section,article,aside,nav,main,hgroup{
display:block;
}
*{
box-sizing: border-box;
}
body {
font-size: 16px;
font-family: "微软雅黑";
}
ul,ol,li {
list-style: none;
}
b,
strong {
font-weight: normal;
}
em,i {
font-style: normal;
}
a,u {
text-decoration: none;
}
h1,h2,h3,h4,h5,h6 {
font-size: 16px;
font-weight: normal;
}
input {
outline: none;
}
img {
display: block; border: 0;
}
fieldset {
border: 0;
}
textarea {
resize: none;
}
.border_none {
border: none !important;
}
.bg_none {
background: none !important;
}
.text_ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
通用jquery-1.8.3.js