JS实现购物车功能(JS案例)
<body onselectstart="return false;">
<template id="temp">
<tr>
<td>
<input type="checkbox" class="check" checked>
</td>
<td>
<img src="images/{src}">{txt}
</td>
<td>{price}</td>
<td>
<span class="reduce">-</span><input class="text" value="1"><span class="add">+</span>
</td>
<td>{subtotal}</td>
<td>
<span class="del">删除</span>
</td>
</tr>
</template>
<div class="box" id="box">
<table>
<thead>
<tr>
<th>
<label>
<input type="checkbox" class="checkAll check" checked>全选
</label>
</th>
<th>商品</th>
<th>单价</th>
<th>数量</th>
<th>⼩计</th>
<th>操作</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<div class="bottom" id="bottom">
<aside>
</aside>
<label>
<input type="checkbox" class="checkAll check" checked>全选
</label>
<span class="delAll">全部删除</span>
<div>已选商品:
<span class="selected" id="num">3</span>件
</div>
<a href="#" class="show">显⽰或隐藏</a>
<div>合计:¥
<span class="total" id="total">7000</span>
</div>
<div class="js">结算</div>
</div>
</div>
CSS代码
body {
background-color: #bcdecf;
div.box {
width: 700px;
margin: 50px auto 0;
}
div.box table {
border-collapse: collapse;
width: inherit;
text-align: center;
background-color: #f6f6f6;
}
div.box table td, div.box th {
border: 1px solid #999;
}
div.box th {
height: 40px;
}
div.box table tbody img {
height: 50px;
}
div.box table tbody tr span {
cursor: default;
}
div.box table tbody tr td:nth-child(2) img {            vertical-align: middle;
}
div.box table tbody tr td:nth-child(4) span {            display: inline-block;
width: 15px;
line-height: 30px;
background-color: #666;
color: #eee;
vertical-align: middle;
}
div.box table tbody tr td:nth-child(4) input {            width: 20px;
height: 20px;
outline: none;
vertical-align: middle;
text-align: center;
}
div.box table tbody tr td:nth-child(6) span {            padding: 4px 10px;
background-color: #999;
color: white;
}
div.box div.bottom {
padding: 15px 0;
margin-top: 15px;
height: 25px;
background-color: white;
display: flex;
justify-content: space-between;
position: relative;
div.box div.bottom span.delAll {
cursor: default;
}
div.box div.bottom div.js {
padding: 0 6px;
background-color: #00A5FF;
color: white;
margin-right: 10px;
cursor: default;
}
div.box div.bottom aside div {
display: inline-block;
}
div.box div.bottom aside div span {
position: absolute;
width: 50px;
line-height: 20px;
padding: 0 5px;
background-color: rgba(255, 255, 255, .4);
color: #333;
font-size: 10px;
margin-left: -60px;
margin-top: 20px;
transform: rotate(30deg);
cursor: pointer;
}
div.box div.bottom aside img {
height: 60px;
vertical-align: middle;
}
div.box div.bottom aside {
position: absolute;
background-color: #0a74cb;
width: 100%;
top: -70px;
padding: 5px;
box-sizing: border-box;
display: none;
}
div.box div. {
display: block;
}
div.box div.bottom aside:after {
position: absolute;
content: "";
border: 10px solid transparent;
border-top-color: #0a74cb;
bottom: -19px;
right: 280px;
}
div.box div.bottom a, div.box div.bottom a:visited {            color: #0b97ff;
text-decoration: none;
}
JS代码
function $(exp){//获取元素
var el;
if (/^#\w+$/.test(exp)){
el=document.querySelector(exp);
} else {
el=document.querySelectorAll(exp);
}
return el;
}
var arr = [];/*表单的数据*/
arr[arr.length] = {src: '1.jpg', txt: 'Casio/卡西欧 EX-TR350', price: 5999.88};
arr[arr.length] = {src: '2.jpg', txt: 'Canon/佳能 PowerShot SX50 HS', price: 3888.50};
arr[arr.length] = {src: '3.jpg', txt: 'Sony/索尼 DSC-WX300', price: 1428.50};
arr[arr.length] = {src: '4.jpg', txt: 'Fujifilm/富⼠ instax mini 25', price: 640.60};
var temp=$('#temp').innerHTML;
var tbody=$('#tbody');
arr.forEach(function (el) {//把数据插⼊到HTML中
tbody.innerHTML += place("{src}", el.src).replace("{txt}", el.txt).replace("{price}", el.price)            .replace("{subtotal}", el.price);
});
var trs=$('#tbody tr');
var box=$('#box');
var aside=$('#bottom aside')[0];
//利⽤事件冒泡的原理,把事件添加给⽗级box
var e=ev||event;
var target=e.target||e.srcElement;//获取当前点击对象
var cls=target.className;
if (cls.indexOf("check")!=-1)cls='check';
switch (cls) {
case 'add'://添加商品数量
var tr=target.parentNode.parentNode;//到点击过那⼀⾏
var lls;
target.previousSibling.value++;//数量那⼀栏的数字加⼀
tds[4].innerText=(tds[2].innerText*target.previousElementSibling.value).toFixed(2);
//修改⼩计⾥⾯的价格
break;
case 'reduce'://减少商品数量
var tr=target.parentNode.parentNode;//到点击过那⼀⾏
var lls;
if (ElementSibling.value!=1) ElementSibling.value--;
//数量那⼀栏减⼀
tds[4].innerText=(tds[2].ElementSibling.value).toFixed(2);
//修改⼩计⾥⾯的价格
break;
case 'text'://直接修改数量那⼀栏input的值borderbox
var tr=target.parentNode.parentNode;
var lls;
tds[4].innerText=(tds[2].innerText*this.value).toFixed(2);
};
break;
case 'del': //删除商品
var tr=target.parentNode.parentNode;
if (confirm('你确定要删除吗?'))
break;
case 'check'://复选框选择商品
chk(target);//执⾏复选框函数
break;
case 'delAll'://删除全部商品
if (confirm('你确定要删除吗?'))
tbody.innerHTML='';
break;
case 'show'://显⽰、隐藏商品
le('on');
break;
case 'cancel':
var Attribute('data-index');
trs[index].cells[0].children[0].checked=false;
}
total();//计算价格
};
var total_all=$('#total');
var num=$('#num');
total();
function total() {//计算价格
var sum=0,number=0;
trs=$('tbody tr');
var str='';
trs.forEach(function (tr,i) {
//遍历每⼀⾏判断,将已选择商品添加到显⽰隐藏⾥⾯
var lls;
if (tds[0].children[0].checked){
sum+=parseFloat(tds[4].innerText);
number+=parseInt(tds[3].children[1].value);
str+=`<div><img src="images/${i+1}.jpg"><span class="cancel" data-index="${i}">取消选择</span></div>`            }
total_all.Fixed(2);
num.innerText=number;
aside.innerHTML=str;
})
}
var checkAll=$('#box .checkAll');
function chk(target) {//复选框判断
var cls=target.className;
var flag = true;
if (cls==='check'){//点击⾮全选复选框
/*当存在⼀个复选框未选中,全选框为false*/
for (var i = 0; i < trs.length; i++) {
var checkbox = trs[i].cells[0].children[0];
if (!checkbox.checked) {
flag = false;
break
}
}
checkAll[0].checked = checkAll[1].checked = flag;
}else {//点击全选复选框,所有复选框的状态保持⼀致
for (var i = 0; i < trs.length; i++) {
var checkbox = trs[i].cells[0].children[0];
checkbox.checked = target.checked;
}
checkAll[0].checked = checkAll[1].checked = target.checked;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。