• 购物车功能(每日一更)


    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="content-type" content="text/html" />
            <meta name="keywords" content="购物车功能" />
            <meta name="description" content="购物车功能" />
            <meta name="author" content="KG" />
            <meta charset="utf-8">
            <title>购物车功能</title>
            <link rel="shortcut icon" href="favicon.icon" />
        </head>
        <style>
            body {
                padding-bottom: 68px;
            }
    
            ul,
            li {
                margin: 0;
                padding: 0;
                list-style: none;
            }
    
            ul {
                 840px;
                margin: 0 auto;
            }
    
            ul::after {
                content: "";
                display: block;
                height: 0;
                clear: both;
                visibility: hidden;
            }
    
            ul li {
                margin: 30px 30px 0;
                 300px;
                box-shadow: 0px 0px 15px 0px #E0E0E0;
                border-radius: 12px;
                margin-bottom: 30px;
                padding: 30px;
                float: left;
            }
    
            ul li img {
                 300px;
                height: 300px;
                margin-bottom: 20px;
            }
    
            ul li p {
                margin-bottom: 20px;
                position: relative;
                padding: 20px 0;
    
            }
    
            ul li p::after {
                position: absolute;
                content: "";
                top: 0;
                left: 0;
                height: 1px;
                 100%;
                border-bottom: 1px solid #999;
                transform: scaleY(0.5);
            }
    
            ul li p::before {
                position: absolute;
                content: "";
                bottom: 0;
                left: 0;
                height: 1px;
                 100%;
                border-bottom: 1px solid #999;
                transform: scaleY(0.5);
            }
    
            ul li div {
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
    
            ul li div span {
                color: #f33b45;
            }
    
            ul li div div span:nth-of-type(2) {
                color: #000;
                font-size: 16px;
                display: inline-block;
                margin: 0 20px;
            }
    
            ul li div div span:last-child,ul li div div span:first-child {
                border: 1px solid #fc5400;
                ;
                padding: 5px 10px;
                color: #fc5400;
                cursor: pointer;
            }
    
            .nav {
                position: fixed;
                bottom: 0;
                left: 0;
                 100%;
                height: 68px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                background-color: #fff;
                box-shadow: 0 0 15px 0 #E0E0E0;
            }
    
            .nav span {
                height: 100%;
                 50%;
                line-height: 68px;
                text-align: center;
                cursor: pointer;
            }
    
            .nav span:last-child {
                padding-right: 30px;
                background-color: #fc5400;
                color: #fff;
    
            }
    
            .nav span:first-child {
                padding-left: 30px;
                color: #fc5400;
            }
        </style>
        <body>
            <div>
                <ul></ul>
            </div>
            <!-- nav -->
            <div class="nav">
                <span class="sum"></span>
                <span onclick="pay()">结算</span>
            </div>
            <script>
                var goodsList = [{
                        id: 0,
                        title: '李宁',
                        price: '198',
                        description: '绝对正品,假一赔十',
                        count:0,
                        img: 'https://cdns.lining.com/postsystem/docroot/images/goods/202002/536281/thumb_display_536281_4.jpg'
                    }, {
                
                        id: 1,
                        title: '安踏',
                        price: '399',
                        description: '绝对正品,假一赔十',
                        count:0,
                        img: 'https://anta-cn-web.obs.myhwclouds.com/shopgoods/1/112011101D/112011101D-4-1.jpg?x-image-process=image/resize,w_800,h_800'
                
                    },
                    {
                        id: 2,
                        title: '耐克',
                        price: '499',
                        description: '绝对正品,假一赔十',
                        count:0,
                        img: 'https://static.nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5/i1-d90b555a-fe92-4644-ac38-2da7c06839d0/air-max-2090-%E5%A5%B3%E5%AD%90%E8%BF%90%E5%8A%A8%E9%9E%8B-mp9K0V.jpg'
                    }
                ];
                var _html = "";
                 // 遍历渲染数据
                 function showDataIntoDom(){
                     for(var i = 0; i < goodsList.length; i++) {
                         console.log('ok');
                         _html += '<li>';
                         _html +='<img src="'+goodsList[i].img+'" />';
                         _html += '<p>'+goodsList[i].description+'</p>';
                         _html += '<div><span>'+goodsList[i].price+'</span><div><span onclick="reduceShoppingCar('+i+')">-</span><span class="count">x'+goodsList[i].count+'</span><span onclick="addShoppingCar('+i+')">+</span></div></div>';
                         _html += '</li>';
                     }
                     document.getElementsByTagName('ul')[0].innerHTML = _html;
                 }
                 showDataIntoDom();
                 // 总额
                 sum();    
                 // 添加商品数量
                function addShoppingCar(index){
                    var currentItem=goodsList[index];
                    currentItem.count++;
                     var countObj=document.getElementsByClassName('count')[index]
                    countObj.innerHTML='x'+currentItem.count;
                    sum();
                }
                // 减少商品数量
                function reduceShoppingCar(index){
                    var currentItem=goodsList[index];
                    if(currentItem.count==0){
                        alert('商品数量不能少于零');
                        return;
                    }
                    currentItem.count--;
                    var countObj=document.getElementsByClassName('count')[index]
                    countObj.innerHTML='x'+currentItem.count;
                    sum();
                }
                // 结算总额
                function sum(){
                    var sum=0;
                    for(var i=0;i<goodsList.length;i++){
                        sum+=goodsList[i].price*goodsList[i].count;
                    }
                    console.log(sum);
                    document.getElementsByClassName('sum')[0].innerHTML='总额:'+sum+'';
                    return sum;
                }
                // 结算
                function pay(){
                    let sumMoney=sum();
                    alert('您将要支付'+sumMoney+'');
                }
            </script>
        </body>
    </html>
  • 相关阅读:
    UVa 10088 (Pick定理) Trees on My Island
    LA 3295 (计数 容斥原理) Counting Triangles
    LA 5846 (计数) Neon Sign
    java是什么?软帝学院告诉你学Java能做什么?Java有什么特性?
    【软帝学院】一套好的java基础教学视频需要哪些有哪些内容
    推荐五个java基础学习网站,小白必备
    学习java设计模式有用吗?懂这六个原则,编程更轻松
    Java是什么?只需5分钟,了解java必须要知道的知识点
    软帝学院:自学java到底难不难?做好这几步,少走3年弯路
    软帝学院:java开发程序很难吗?学会这十步,5分钟搞定一个程序
  • 原文地址:https://www.cnblogs.com/nimon-hugo/p/12719335.html
Copyright © 2020-2023  润新知