原生js实现简易购物车效果

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
<script type="text/javascript" src="js/tool.js"></script>
<style type="text/css">
	*{margin: 0;padding: 0;}
	ul{list-style: none;}
	a{text-decoration: none;}
	.box{
		width: 1200px;
		border: 1px solid;
		margin: 50px auto 0;
	}
	.box ul{
		overflow: hidden;
	}
	.box ul li{
		width: 260px;
		height: 100px;
		font-size: 12px;
		float: left;
		margin-right: 53px;
		margin-bottom: 15px;
	}
	.box .product a img{
		float: left;
		width: 98px;
		height: 98px;
		border: 1px solid #ccc;
		margin-right: 10px;
	}
	.box .product .txt{
		float: left;
		width: 150px;
		height: 36px;
		line-height: 18px;
		overflow: hidden;
	}
	.box .product .price{
		float: left;
		width: 150px;
		color: #d91f20;
		font-size: 14px;
		margin-top: 5px;
		overflow: hidden;
	}
	.box .product .add_to_cart{
		float: left;
		width: 105px;
		height: 30px;
		border: 1px solid #ccc;
		margin-top: 8px;
		text-align: center;
		line-height: 30px;
	}
	.header{
		width: 1200px;
		/*height: 100px;*/
		border: 1px solid #ccc;
		margin: 0 auto;
	}
	.header .title{
		display: flex;
		justify-content: space-around;
		height: 30px;
		line-height: 30px;
		border: 1px solid #ccc;
		background: #f3f3f3;
	}
	.header .cart_list_box{
		max-height: 306px;
		overflow: auto;
	}
	.header .cart_list{
		border: 1px solid #ccc;
		display: flex;
		justify-content: space-between;
		border-bottom: none;
	}
	.header .cart_list li{
		float: left;
		height: 50px;
		width: 200px;
		font-size: 14px;
		line-height: 50px;
		text-align: center;
		overflow: hidden;
		background: #fff4e8;
	}
	.header .cart_list li .img{
		width: 50px;
		height: 50px;
	}
	.product_total{
		width: 1200px;
		margin: 0 auto;
		overflow: hidden;
		outline: 1px solid #ccc;
	}
	.product_total .fr{
		float: right;
		width: 280px;
		height: 50px;
	}
	.product_total .fr p{
		float: left;
		line-height: 50px;
		color: #ccc;
		font-size: 12px;
	}
	.product_total .fr .calc{
		float: right;
		width: 110px;
		height: 50px;
		background: #e64346;
		color: #fff;
		font-weight: bold;
		text-align: center;
		line-height: 50px;
		font-size: 18px;
	}
	.product_total .fr p #total{
		color: orange;
		font-size: 22px;
	}
	.product_total .fr p i{
		font-size: 20px;
		color: orange;
		font-style: normal;
	}
	.product_total .fl #removeAll{
		float: left;
		margin-left: 500px;
		margin-top: 4px;
		width: 110px;
		height: 40px;
		background: #fff8f8;
		border: 1px solid #ffdfdf;
		color: #999;
		text-align: center;
		line-height: 40px;
		font-size: 16px;
	}
</style>
</head>
<body>
	<div class="header">
		<ul class="title">
			<li>编号</li>
			<li>图片</li>
			<li>标题</li>
			<li>数量</li>
			<li>单价</li>
			<li>操作</li>
		</ul>
		<div class="cart_list_box">

		</div>
	</div>
	<div class="product_total">
		<div class="fl">
			<a href="javascript:void(0)" id="removeAll">清空购物车</a>
		</div>
		<div class="fr">
			<p>合计:<i>¥</i><span id="total">00.00</span></p>
			<a href="javascript:void(0)" class="calc">去结算</a>
		</div>
	</div>

	<div class="box">
		<h3>购买了该商品的用户还购买了</h3>
		<ul class="con">

		</ul>
	</div>
	<ul class="conModel">
		<li style="display: none">
			<div class="id" style="display:none">1</div>
			<a href=""><img class="img" src="images/01.jpg"></a>
			<p class="txt">益达(Extra)木糖醇无糖口香糖冰凉薄荷70粒98单瓶装</p>
			<span class="price">¥13.90</span>
			<a href="javascript:void(0);" class="add_to_cart">加入购物车</a>
		</li>
	</ul>
<script>
	var json = [
		{"id":"1","img":"images/01.jpg","txt":"益达(Extra)木糖醇无糖口香糖冰凉薄荷70粒98单瓶装","price":"¥13.90"},
		{"id":"2","img":"images/02.jpg","txt":"益达(Extra)尊享护齿56g*3瓶京东定制装(草本精华+冰","price":"¥24.90"},
		{"id":"3","img":"images/03.jpg","txt":"绿箭(DOUBLEMINT)无糖薄荷糖冰柠薄荷味35粒23.8g","price":"¥7.50"},
		{"id":"4","img":"images/04.jpg","txt":"益达(Extra)木糖醇无糖口香糖香浓蜜瓜40粒56g单瓶装","price":"¥8.80"},
		{"id":"5","img":"images/05.jpg","txt":"益达(Extra)无糖口香糖沁凉薄荷12片32g单盒装","price":"¥5.50"},
		{"id":"6","img":"images/06.jpg","txt":"益达木糖醇无糖口香糖瓶装56g/瓶 益达口香糖 休闲零食","price":"¥8.80"},
		{"id":"7","img":"images/07.jpg","txt":"益达木糖醇无糖口香糖瓶装56g/瓶*9口味组合休闲零食","price":"¥75.60"},
		{"id":"8","img":"images/08.jpg","txt":"益达 无糖口香糖70粒摇一摇四口味合装 赠益达西瓜味24","price":"¥52.90"}
	];
	var parent = $(".con",$(".box")[0])[0];
	for(var i = 0,len = json.length; i < len; i++){
		var clone = $("li",$(".conModel")[0])[0].cloneNode(true);
		if((i+1) % 4 === 0){
			clone.style.marginRight = "0";
		}
		clone.style.display = "block";
		clone.className = "product";
		clone.children[0].innerHTML = json[i].id;
		clone.children[1].children[0].src = json[i].img;
		clone.children[2].innerHTML = json[i].txt;
		clone.children[3].innerHTML = json[i].price;
		parent.appendChild(clone);
	}
	// 动态添加购物车列表
	var add_to_cart = $(".add_to_cart",$(".con")[0]);
	for(var j = 0,cartLen = add_to_cart.length; j < cartLen; j++){
		add_to_cart[j].onclick = function(){
			var cart_list = $(".cart_list",$(".header")[0]);
			for(var k =0, cart_list_len = cart_list.length; k < cart_list_len; k++){
				if(this.parentNode.children[0].innerHTML === $("li",$(".cart_list")[k])[0].innerHTML ){
					var num = ++cart_list[k].children[3].innerHTML;
					var prices2 = Number(cart_list[k].children[4].innerHTML.slice(1));
					$("#total").innerText = (Number($("#total").innerText) + prices2).toFixed(2);
					return;
				}
			}
			var clone = $(".title",$(".header")[0])[0].cloneNode(true);
			clone.className = "cart_list";
			clone.children[0].innerHTML = this.parentNode.children[0].innerHTML;
			clone.children[1].innerHTML = this.parentNode.children[1].innerHTML;
			clone.children[2].innerHTML = this.parentNode.children[2].innerHTML;
			clone.children[3].innerHTML = "1";
			clone.children[4].innerHTML = this.parentNode.children[3].innerHTML;
			clone.children[5].innerHTML = "<a href='javascript:void(0)' class='remove'>删除</a>";
			$(".cart_list_box",$(".header")[0])[0].appendChild(clone);
			var prices1 = Number(clone.children[4].innerHTML.slice(1));
			$("#total").innerText = (Number($("#total").innerText) + prices1).toFixed(2);
			// var id = this.parentNode.children[0].innerHTML,
			// 	img = this.parentNode.children[1].innerHTML,
			// 	txt = this.parentNode.children[2].innerHTML,
			// 	price = this.parentNode.children[3].innerHTML;
			// $(".header")[0].innerHTML += '<ul class="cart_list">'
			// 								+	'<li>'+id+'</li>'
			// 								+	'<li>'+img+'</li>'
			// 								+	'<li>'+txt+'</li>'
			// 								+	'<li>1</li>'
			// 								+	'<li>'+price+'</li>'
			// 								+	'<li><a class="remove" href="javascript:void(0)">删除</a></li>'
			// 								+'</ul>';

			// 删除购物车列表内指定商品
			for(var l = 0,remove_len = $(".remove").length; l < remove_len; l++){
				$(".remove")[l].onclick = function(){
					if(confirm("确认删除该商品?")){
						var price = (this.parentNode.parentNode.children[4].innerHTML).slice(1)
						var num = this.parentNode.parentNode.children[3].innerHTML;
						var removePrices = price * num;
						$("#total").innerText = (Number($("#total").innerText) - removePrices).toFixed(2);
						this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
					}
				}
			}
		}
	}

	// 清空购物车
	$("#removeAll").onclick = function(){
		for(var i = 0,len = $(".cart_list_box")[0].children.length; i < len; i++){
			$(".cart_list_box")[0].removeChild($(".cart_list_box")[0].children[0]);
		}
		$("#total").innerText = "00.00";		
	}
</script>
</body>
</html>
以上获取元素用了自己封装的选择器工具,在tool.js内

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
js加入购物车抛物线动画与购物车效果特效,亲测可用, 当您在电商购物网站浏览中意的商品时,您可以点击页面中的“加入购物车”按钮即可将商品加入的购物车中。本文介绍借助一款基于jQuery的动画插件,点击加入购物车按钮时,实现商品将飞入到右侧的购物车中的效果。 HTML 首先载入jQuery库文件和jquery.fly.min.js插件。 复制代码 代码如下: 接着,将商品信息html结构布置好,本例中,我们用四个商品并排布置,每个商品box中包括有商品图片、价格、名称以及加入购物车按钮等信息。 复制代码 代码如下: ¥3499.00 LG 49LF5400-CA 49寸IPS硬屏富贵招财铜钱设计 加入购物车 ¥3799.00 Hisense/海信 LED50T1A 海信电视官方旗舰店 加入购物车 ¥¥3999.00 Skyworth/创维 50E8EUS 8核4Kj极清酷开系统智能液晶电视 加入购物车 ¥6969.00 乐视TV Letv X60S 4核1080P高清3D安卓智能超级电视 加入购物车 然后,我们还需要在页面的右侧加上购物车以及提示信息。 复制代码 代码如下: 购物车 已成功加入购物车! CSS 我们使用CSS先将商品排列美化,然后设置右侧购物车样式,具体请看代码: 复制代码 代码如下: .box{float:left; width:198px; height:320px; margin-left:5px; border:1px solid #e0e0e0; text-align:center} .box p{line-height:20px; padding:4px 4px 10px 4px; text-align:left} .box:hover{border:1px solid #f90} .box h4{line-height:32px; font-size:14px; color:#f30;font-weight:500} .box h4 span{font-size:20px} .u-flyer{display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;} .m-sidebar{position: fixed;top: 0;right: 0;background: #000;z-index: 2000;width: 35px;height: 100%;font-size: 12px;color: #fff;} .cart{color: #fff;t
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值