为了账号安全,请及时绑定邮箱和手机立即绑定

改版后前端购物车系统

图片描述

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>shopping</title>
        <link rel="stylesheet" href="css/shopping.css" />
    </head>
    <body>      
        <div id="middle">
            <div id="middle2">
              <!--这是商品-->
            </div>
            <div class="result">
                <div class="allText">全选:</div>
                <div class="all"></div>
                <div class="money">
                    <div class="rmb">¥:<span class="price">0</span></div>
                    <div class="btn">结算</div>
                </div>
            </div>
        </div>
        <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="js/jquery-1.7.2.min.js"></script>
        <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="js/shopping.js"></script>
    </body>
</html>

*{
    margin: 0;
    padding: 0;
    font-family: "微软雅黑";
}

#middle{
    width:500px;
    height: 800px;
    border:1px solid black;
    margin: 0 auto;
    margin-top: 30px;
    position: relative;
}

.one{
    width: 100%;
    height: 150px;
    border-bottom: 1px solid black;
}

.quan{
    width:15px;
    height: 15px;
    line-height: 15px;
    text-align: center;
    color:#fff;
    border:1px solid red;
    border-radius:50%;
    position: relative;
    top:70px;
    left:15px;
    cursor: pointer;
}

.picture{
    width:120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
    font-size: 20px;
    background:goldenrod;
    float:left;
    margin-left: 100px;
}

.text{
    width:170px;
    height: 112px;
    font-size: 16px;
    padding-top: 8px;
    float:right;
    margin-right: 100px;
}

.left,.right{
    float:left;
    width:25px;
    height: 25px;
    font-size: 18px;
    border:1px solid red;
    position: relative;
    top:35px;
    cursor: pointer;
    text-align: center;
    margin-right: 10px;
}

.num{
    float:left;
    width:25px;
    height: 25px;
    font-size: 18px;
    border:1px solid red;
    position: relative;
    top:35px;
    text-align: center;
    margin-right: 10px;
}

.result{
    width:100%;
    height: 50px;
    line-height: 50px;
    border-top:2px solid brown;
    position: absolute;
    bottom: 0px;
}

.allText{
    float: left;
    font-size:16px ;
    padding-left: 10px;
}

.all{
    width:15px;
    height: 15px;
    line-height: 15px;
    text-align: center;
    color:#fff;
    border:1px solid red;
    border-radius:50%;
    float: left;
    cursor: pointer;
    margin-top:17px;
    margin-left: 10px;
}

.money{
    float:right;
    width:160px;
    height: 100%;
}

.rmb{
    font-size: 20px;
    float: left;
}

span.price{
    margin-left: 5px;
}

.btn{
    width:100px;
    height: 100%;
    text-align: center;
    background:brown;
    color:#fff;
    font-size: 18px;
    line-height: 50px;
    float: right;
    cursor: pointer;
}

#maskphoto{
    width: 100%;
    height: 100%;
    position: absolute;
    top:0;
    background:#000;
    opacity: 0.6;
    z-index: 99;
    display: none;
}

.alert{
    width:0;
    height: 0;
    position: fixed;
    left:50%;
    right:50%;
    top:50%;
    bottom: 50%;
    z-index:100;
}

.alert2{
    width:300px;
    height: 160px;
    border:1px solid brown;
    border-radius: 5px;
    margin-left: -150px;
    margin-top: -80px;
    background:#fff;
    display: none;
}

.mmText{
    width: 95%;
    height: auto;
    margin: 0 auto;
    text-align: center;
    padding: 20px 0;
    padding-top: 30px;
}

.bottom{
    width:90%;
    height: 40px;
    margin: 0 auto;
    margin-top: 30px;
}

.btnLeft{
    width:100px;
    height: 40px;
    float:left;
    background: brown;
    line-height: 40px;
    text-align: center;
    font-size: 17px;
    border-radius: 5px;
    color:#fff;
    cursor: pointer;
}

.btnRight{
    width:100px;
    height: 40px;
    float:right;
    background: brown;
    line-height: 40px;
    text-align: center;
    font-size: 17px;
    border-radius: 5px;
    color:#fff;
    cursor: pointer;
}

var all = 0;//全选
var k = 0; 
$(function(){
    twh();
});

function twh(){
     var maindiv=$("#middle2"); 
         maindiv.html("");
         var str = '';
       for(var i=1;i<5;i++){
                str += '<div class="one" id="'+i+'">'
                     + '<div class="quan" onclick="quan('+i+')" alt="0"></div>'
                     + '<div class="picture">商品'+i+'元</div>'
                     + '<div class="text">'
                     + '<div>这是件商品这是件商品这是件商品</div><div onclick="left('+i+')" class="left">-</div><div class="num">0</div><div onclick="right('+i+')" class="right">+</div>'
                     + '</div>'
                     + '</div>'
       }
       maindiv.html(str);
}

function quan(i){
     var result = $(".price").text(); //获取总金额
     var num = $("#"+i).find(".num").text(); //获取相对应的数量
    if($("#"+i).find(".quan").attr("alt")==0){     
       $("#"+i).find(".quan").css("background","red").attr("alt","1").text("√");           
       $(".price").text(parseInt(result)+i*num);
       k++;
       if(k==4){
        $(".all").css("background","red").text("√");
        all=1;
       }
    }else{
       $("#"+i).find(".quan").css("background","none").attr("alt","0").text("");
       $(".price").text(parseInt(result)-i*num);
       $(".all").css("background","none");//全选
       k--;
       all=0;
    }
}

//点击减少
function left(i){
    var num = $("#"+i).find(".num").text();//获取相对应的数量
    var result = $(".price").text(); //获取总金额
    num--;
    if(num<0){
        alert("商品个数不能小于0!");
        return false;
    }
    $("#"+i).find(".num").text(num);
    if($("#"+i).find(".quan").attr("alt")==1){
        $(".price").text(parseInt(result)-i);
    }   
}

//点击增加
function right(i){
    var num = $("#"+i).find(".num").text();//获取相对应的数量
    var result = $(".price").text(); //获取总金额
    num++;
    $("#"+i).find(".num").text(num);
    if($("#"+i).find(".quan").attr("alt")==1){
        $(".price").text(parseInt(result)+i);
    }
}

//点击全选
$(function(){
   $(".all").on("click",function(){
         if(all==0){
           for(var i=1;i<5;i++){
               var num = $("#"+i).find(".num").text();//获取相对应的数量
               var result = $(".price").text(); //获取总金额
               var b = $("#"+i).find(".quan").text();
               if(b=="√"){
                 b=i;
                 var price = i*num;
                 $(".price").text(parseInt(result)+i*num-price);
               }else{
                 $(".price").text(parseInt(result)+i*num);
               }           
           }
                $(this).css("background","red").text("√");
                $(".quan").attr("alt","1").css("background","red").text("√");
                all=1;
                k=4;
                return false;          
         }
         if(all==1){
                $(".quan").attr("alt","0").css("background","none").text("");
                $(this).css("background","none").text("");
                $(".price").text(0);
                all=0;
                k=0;
                return false;
         }               
     });
});
点击查看更多内容
5人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消