javascript购物车

购物车

<style type="text/css">
			body,html{
				background-color: whitesmoke;
			}
			.shopBox{
				width: 425px;
				height: 375px;
				background-color: white;
				margin: 0 auto;
				position: relative;
			}
			.shopLine{
				/*background-color: pink;*/
				height: 70px;
				padding: 15px;
				position: relative;
				box-shadow: 3px 3px 3px #dddddd;
				margin-bottom: 3px;
			}
			.shopLine img{
				float: left;
				width: 70px;
				height: 70px;
			}
			.inputBox{
				float: left;
				margin-top: 30px;
				margin-right: 10px;
			}
			.shopDetails{
				float: left;
				margin-left: 20px;
			}
			.shopDetails span{
				display: block;
				margin-bottom: 5px;
			}
			.shopDetails span:nth-child(2){
				color: gray;
				font-size: 13px;
				margin: 8px 0;
			}
			.shopDetails span:nth-child(3){
				color: #e4393e;
				font-size: 16px;
			}
			.shopCalc{
				position: absolute;
				bottom: 10px;
				right: 20px;
				border: 1px solid #9A9A9A;
				line-height: 30px;
				text-align: center;
				border-radius: 5px;
			}
			.calcDown,.calcUp{
				display: inline-block;
				width: 30px;
				height: 30px;
				cursor: pointer;
			}
			.calcNum{
				width: 28px;
				height: 28px;
				border: none;
				text-align: center;
				border-left: 1px solid #9A9A9A;
				border-right: 1px solid #9A9A9A;
				outline: none;
			}
			.shopCart{
				position: absolute;
				width: 100%;
				height: 70px;
				background-color: pink;
				bottom: 0;
				left: 0;
			}
			.shopCon{
				position: relative;
				padding: 0 15px;
			}
			.totalPrices{
				float: left;
				margin-left: 50px;
				line-height: 80px;
			}
		</style>
	</head>
	<body>
		<!--
			1、全选按钮的监听
			2、每一个商品中的复选框 都应该被监听
			3、单个商品的数量的加法
			4、单个商品的数量的减法
			5、数量的额手动输入
			6、总价的联动
		-->
		
		
		<div id="app">
			<div class="shopBox">
				<!--第一个商品-->
				<div class="shopLine">
					<div class="inputBox">
						<input class="sigChecked" type="checkbox" />
					</div>
					<img src="https:\/\/fjlyl.xyz\/assets\/hairDye\/products (1).jpg" />
					<div class="shopDetails">
						<span>施华蔻伊采染发膏60ml</span>
						<span>规格:50克</span>
						<span>¥<label class="sigPrices">50</label></span>
					</div>
					<div class="shopCalc">
						<span  class="calcDown">-</span>
						<input class="calcNum" value="1" />
						<span  class="calcUp">+</span>
					</div>
				</div>	
				<!--第二个商品-->
				<div class="shopLine">
					<div class="inputBox">
						<input class="sigChecked" type="checkbox" />
					</div>
					<img src="https:\/\/fjlyl.xyz\/assets\/hairDye\/products (1).jpg" />
					<div class="shopDetails">
						<span>施华蔻伊采染发膏60ml</span>
						<span>规格:50克</span>
						<span>¥<label class="sigPrices">69</label></span>
					</div>
					<div class="shopCalc">
						<span  class="calcDown">-</span>
						<input class="calcNum" value="1" />
						<span  class="calcUp">+</span>
					</div>
				</div>	
				<!--底部-->
				<div class="shopCart">
					<div class="shopCon">
						<div class="inputBox">
							<input class="allChecked" type="checkbox" />全选
						</div>
						<div class="totalPrices">
							总价:¥ <span class="allPrices">0</span>
						</div>
					</div>
				</div>
				
			</div>
		</div>
	</body>
	<script type="text/javascript">
		//商品数组
		var shopLine = document.getElementsByClassName('shopLine');
		//价格数组
		var sigPrices = document.getElementsByClassName('sigPrices');
		//总价
		var allPrices = document.getElementsByClassName('allPrices')[0];
		//减法按钮数组
		var calcDown = document.getElementsByClassName('calcDown');
		//加法按钮数组
		var calcUp = document.getElementsByClassName('calcUp');
		//商品的数量的输入框数组
		var calcNum = document.getElementsByClassName('calcNum');
		//全选按钮
		var allChecked = document.getElementsByClassName('allChecked')[0];
		//单个勾选按钮的数组
		var sigChecked = document.getElementsByClassName('sigChecked');
		
		//全选按钮的监听onchange
		allChecked.onchange = function () {
			var checked = allChecked.checked;//全选按钮的已经被改变的那个值
			//全部的单个复选框  都应该和 全选按钮的值是一致的
			for(var i=0;i<sigChecked.length;i++){
				sigChecked[i].checked = checked;
			}
			arrCheck();
			totalPrices();
		}
		
		for(var i=0;i<shopLine.length;i++){
			//每个单个勾选的监听onchange
			sigChecked[i].onchange = function() {
				//arrChecked  这个数组中相对应的布尔值  来去判断  全选按钮是否被勾选上
				arrCheck();
				var checked = true;//假定全选按钮的值为true
				for(var i=0;i<arrChecked.length;i++){
					if(arrChecked[i] == false){
						checked = false;//只要有一个没选上  全选按钮 就没有被选上
						break;
					}
				}
				allChecked.checked = checked;
				totalPrices();
			}
			//加法
			calcUp[i].index = i;//
			calcUp[i].onclick = function () {
				//calcNum[this.index].value
				var num = parseInt(calcNum[this.index].value) + 1;
				
				calcNum[this.index].value = num;
				totalPrices();
			}
			//减法
			calcDown[i].index = i;
			calcDown[i].onclick = function () {
				//字符串 数值的时候  它是有隐式转换的嘛  '3'-1
				var num = parseInt(calcNum[this.index].value);
				num = num == 1?1:num - 1;
				calcNum[this.index].value = num;
				totalPrices();
			}
			//手动输入
			calcNum[i].index = i;
			//onblur监听 input框 脱离焦点的事件
			calcNum[i].onblur = function () {
				var num = calcNum[this.index].value;
				//判断是否为数字
				num = isNaN(num)? 1 : Math.ceil(num);
				
				num = num < 0 ? 1 : num;
				
				calcNum[this.index].value = num;
				totalPrices();
			}
		}
		//每次把变动的sigChecked的每个布尔值都重新获得
		var arrChecked = [];
//		var cart = [
//				{shopId:1,isChecked:true,prices:50,shopName:'name0',num:5},
//				{shopId:2,isChecked:true,prices:69,shopName:'name1',num:5}
//			]
		//一开始就刷新数组里的值
		arrCheck();
		//每个单个勾选的布尔值的数组
		function arrCheck () {
			for(var i=0;i<sigChecked.length;i++){
				arrChecked.splice(i,1,sigChecked[i].checked);
			}
			console.log(arrChecked);
			//totalPrices();
		}
		
		//计算总价  每处有变动就需要做计算
		function totalPrices () {
			var totalPrices = 0;
			for(var i=0;i<arrChecked.length;i++){
				if(arrChecked[i]){
					//单价*数量
					totalPrices += parseFloat(sigPrices[i].innerHTML) * calcNum[i].value
				}
			}
			allPrices.innerHTML = totalPrices;
		}
		
	</script>

效果图
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值