我个人的一个购物车的记录方法
购物车
成品
在成品页面中顶部的横条,底部的立刻预约,商品的内容,角标=商品单价,结算页面,底部结算内的商品总价和商品数量。
其中在进行内容的布置的时候首先将首页和立刻预约这个给做出来固定好样式,然后在设置一个div用来存储数组并且设置好样式在这一步将角标隐藏起来。
结算页面里也是一个内容的输出和外面的商品是一样的都是多了两个button按钮在进行调整样式HTML和css的内容输出完毕。
其中角标,结算页面,底部结算都是隐藏起来。在做完全部的页面的内容的样式的时候将角标,底部结算和结算页面的这三个将其隐藏起来。
初始
HTML和css的页面做好后,就是将js数据输出HTML页面上去,第一个输出的就是商品的全部内容,利用for循环循环输出到页面上。
结算页面 上的内容也是要用到for循环,循环输出到页面上。
第二步
添加点击事件。
给商品的整个div都添加一个点击事件里面要进行底部结算的显示,角标数量的输出和显示,以及结算页面的输出。
最后进行代码的呈现:
<style type="text/css">/* css样式 */
body {
margin: 0;
padding: 0;
background-color: #e8e8e8;
height: 5000px;
}
#head {
width: 100%;
background-color: #00ffff;
height: 200px;
color: #FFFFFF;
margin-top: -34px;
text-align: center;
position: fixed;
}
#head h1 {
font-size: 75px;
padding-top: 15px;
}
#bottom {
background-color: #FFFFFF;
width: 100%;
height: 150px;
color: #FFFFFF;
margin-top: 1000px;
text-align: center;
display: inline-block;
position: fixed;
justify-content: flex-start;
}
#bottom img {
width: 100px;
}
#bottom p {
width: 300px;
}
#bottom h1 {
width: 200px;
}
.shangpin {
width: 100%;
line-height: 5px;
}
.shangpin img {
width: 100%;
margin-top: 200px;
}
.shangpin p {
font-size: 30px;
}
.shangpin span {
font-size: 50px;
position: fixed;
margin-top: -200px;
border: #FF0000 1px solid;
background-color: red;
padding: 0px 15px 0px 15px;
border-radius: 50%;
}
.mmd {
width: 45%;
margin-left: 3%;
display: inline-block;
}
.mmd span {
width: 40px;
height: 40px;
color: #000000;
border-radius: 50%;
text-align: center;
position: absolute;
font-size: 30px;
padding-top: 15px;
padding-bottom: -30px;
margin-top: -550px;
background-color: aqua;
margin-left: -25px;
display: none;
}
.colors {
color: #FF0000;
}
.last_nav {
background-color: #FFFFFF;
width: 100%;
position: fixed;
bottom: 0;
display: none;
}
.last_nav img {
width: 15%;
}
.total {
font-size: 40px;
position: fixed;
margin-top: -200px;
}
.total p {
border: #FF0000 1px solid;
background-color: red;
padding: 0px 15px 0px 15px;
border-radius: 50%;
}
.total_prices {
position: fixed;
display: inline-block;
font-size: 50px;
margin-left: 150px;
}
#total_price {
margin-left: 25px;
color: #FF0000;
font-size: 40px;
}
.order {
background-color: aqua;
width: 30%;
position: fixed;
font-size: 50px;
margin-top: -150px;
margin-left: 70%;
}
.orders {
background-color: aqua;
width: 100%;
position: fixed;
font-size: 50px;
bottom: 0;
}
#none {
position: fixed;
width: 100%;
height: 1000px;
bottom: 0;
background-color: #FFFFFF;
display: none;
}
#none_n {
display: flex;
justify-content: space-around;
}
#none_n h1 {
color: #00ff00;
font-size: 50px;
}
#none_n a {
display: inline-block;
text-decoration: none;
padding-top: 45px;
}
#none_n span {
font-size: 50px;
color: #ff0000;
padding-top: 45px;
}
.none_m {
margin-left: 10%;
width: 100%;
font-size: 50px;
display: none;
}
.none_m input {
border-style: none;
outline: none;
width: 50px;
height: 50px;
font-size: 50px;
}
.none_m img {
width: 10%;
}
.none_m button {
width: 50px;
height: 50px;
font-size:50px;
}
</style>
<body>
<div id="head">
<h1>首页</h1>
</div>
<div class="shangpin"></div>
<div id="none">
<div id="none_n">
<a href=""><span>清空</span></a>
<h1>结算页面</h1>
<span onclick="mms()">关闭</span>
</div>
<div class="none_b"></div><!-- 结算页面的内容 -->
</div>
<div class="orders">
<p style="line-height: 50%;text-align: center;">立即预约</p>
</div>
<div class="last_nav">
<img src="./img/gwc/购物车.png" onclick="mma()">
<div class="total">
<p id="sum">0</p>
</div>
<div class="total_prices">
预估价格¥<span id="total_price">0</span><br>
不含运费
</div>
<div class="order">
<p style="line-height: 50%;text-align: center;" onclick="alertfun()">立即预约</p>
</div>
</div>
</body>
<script type="text/javascript">
var clothes = [{
img: "img/gwc/u=1236984783,3291646751&fm=26&gp=0.jpg",
name: "YSL唇釉",
price: 320,
buttum: '-',
num: 0,
buttun: '+',
}, {
img: "./img/gwc/u=1825026782,2394796598&fm=26&gp=0.jpg",
name: "Amani香水",
price: 199,
buttum: '-',
num: 0,
buttun: '+',
}, {
img: "./img/gwc/u=2744336323,3645277210&fm=26&gp=0.jpg",
name: "Mac口红",
price: 170,
buttum: '-',
num: 0,
buttun: '+',
}, {
img: "./img/gwc/u=3071406061,411961126&fm=26&gp=0.jpg",
name: "TF口红",
price: 450,
buttum: '-',
num: 0,
buttun: '+',
}, {
img: "./img/gwc/下载.jpg",
name: "Dior999",
price: 310,
buttum: '-',
num: 0,
buttun: '+',
}];
var mmd = document.getElementsByClassName("shangpin")[0];
for (var i = 0; i < clothes.length; i++) {
var div = document.createElement('div'); //创建div
var img = document.createElement('img'); //创建img
var p = document.createElement('p'); //创建p标签
var m = document.createElement('p'); //创建p标签单价
num = document.createElement('span'); //创建span标签角标
div.className = 'mmd'; //给创建的div+class名称
img.src = clothes[i].img; //把img输出到img标签
p.innerHTML = clothes[i].name; //把name输出到p标签
m.innerHTML = clothes[i].price; //把price输出到p标签
m.className = 'colors';
num.className = "mmw";
num.innerHTML = 0;
// console.log(clothes[i].num)
div.appendChild(img); //把img插入到div
div.appendChild(p); //把p标签插入到div
div.appendChild(m); //把p标签插入到div
div.appendChild(num); //把span标签插入到div
mmd.appendChild(div); //把小div中插入大的div
// console.log(mmd)
(function(i) {
document.getElementsByClassName('mmd')[i].onclick = function() {
var summ = document.getElementById("sum").innerHTML - 0; //总数
var total_prices = document.getElementById("total_price").innerHTML - 0; //总价
summ++;
clothes[i].num++;
document.getElementsByClassName('mmw')[i].innerHTML = clothes[i].num;
console.log(clothes[i].num);
document.getElementById("sum").innerHTML = summ;
document.getElementById("total_price").innerHTML = total_prices += clothes[i].price;
document.getElementsByClassName('last_nav')[0].style.display = "block";
document.getElementsByClassName("mmw")[i].style.display = "block";
document.getElementsByClassName("none_m")[i].style.display = "block";
document.getElementsByClassName("none_z")[i].innerHTML = clothes[i].num;
}
})(i)
}
var none_b = document.getElementsByClassName("none_b")[0];
var none_m = "";
for (var i = 0; i < clothes.length; i++) {
none_m += '<div class="none_m">';
none_m += '<img src=' + clothes[i].img + '>' + '<span>' + clothes[i].name + '</span>' +
'<button οnclick="pdh(' + i + ')">' + clothes[i].buttum + '</button>' +
'<span class="none_z">' + clothes[i].num +'</span>' +
'<button οnclick="asb(' + i + ')">' + clothes[i].buttun + '</button>';
none_m += '</div>';
none_b.innerHTML = none_m;
}
function asb(i) {
var summ = document.getElementById("sum").innerHTML - 0; //总数
var total_prices = document.getElementById("total_price").innerHTML - 0; //总价
summ++;
clothes[i].num++;
document.getElementsByClassName('mmw')[i].innerHTML = clothes[i].num;
document.getElementById("sum").innerHTML = summ;
document.getElementById("total_price").innerHTML = total_prices += clothes[i].price;
document.getElementsByClassName("mmw")[i].style.display = "block";
document.getElementsByClassName("none_m")[i].style.display = "block";
document.getElementsByClassName("none_z")[i].innerHTML = clothes[i].num;
}
function pdh(m) {
var summ = document.getElementById("sum").innerHTML - 0; //总数
var total_prices = document.getElementById("total_price").innerHTML - 0; //总价
summ--;
clothes[m].num--;
document.getElementsByClassName('mmw')[m].innerHTML = clothes[m].num;
document.getElementById("total_price").innerHTML = total_prices -= clothes[m].price;
document.getElementsByClassName("none_z")[m].innerHTML = clothes[m].num;
if (clothes[m].num == 0) {
// alert("hjg")
document.getElementById("sum").innerHTML = summ;
document.getElementsByClassName('none_m')[m].style.display = "none";
document.getElementsByClassName("mmw")[m].style.display = "none";
document.getElementsByClassName('last_nav')[0].style.display = "none";
}
}
function mma() {//结算页面显示
var none = document.getElementById("none");
none.style.display = "block";
}
function mms() {//结算页面隐藏
var none = document.getElementById("none");
none.style.display = "none";
document.getElementsByClassName('last_nav')[0].style.display = "none";
}
function alertfun() {
var total_prices = document.getElementById("total_price").innerHTML - 0; //总价
alert("您已选择了" + total_prices + "元的商品")
document.getElementsByClassName('last_nav')[0].style.display = "none";
}
</script>