JS實(shí)現(xiàn)簡單計(jì)數(shù)器

使用HTML CSS和JavaScript實(shí)現(xiàn)簡單計(jì)數(shù)器,有加、減和零三個(gè)按鈕,分別實(shí)現(xiàn)加一、減一和歸零操作。下面先看一下效果圖。

在這里插入圖片描述

HTML代碼

<div class="body">
            <div class="text">
                <font>Counter</font>
            </div>
            <div class="count" >
                <span id="demo" class="ft">
                    2
                </span>
            </div>
            <div class="btn">
                <button type="button" onclick="add()" class="btn1">+</button>
                <button type="button" onclick="zero()" class="btn2">零</button>
                <button type="button" onclick="sub()" class="btn1">-</button>

            </div>
        </div>

CSS代碼

            .body {
                width: 300px;
                height: 500px;
                background-color: #211d5a;
                border-radius: 20px;
                display: flex;
                flex-direction: column;
                align-items: center;
            }
            
            .text {
                font-size: 24px;
                color: white;
                margin-top: 60px;
                text-shadow: 2px 2px 2px #fff;
            }
            
            .count {
                position: relative;
                width: 200px;
                height: 200px;
                margin-top: 60px;
                border: 10px solid;
                border-color: rgb(79, 59, 156);
                border-radius: 50%;
                display: flex;
            }
            
            .ft {
                font-size: 100px;
                color: #fff;
                /*left: 50%;
                margin-left: -102px;
                margin-top: 40px;*/
                margin: auto;
            }
            
            .btn {
                width: 220px;
                display: flex;
                /*flex-direction: row;*/
                justify-content: space-between;
                margin-top: 60px;
            }
            
            .btn1 {
                width: 50px;
                height: 30px;
                border: 0px;
                border-radius: 4px;
                background-color: rgb(79, 59, 156);
                font-size: 20px;
                color: #efdaff;
            }
            
            .btn2 {
                width: 50px;
                height: 30px;
                border: 0px;
                border-radius: 4px;
                background-color: rgb(79, 59, 156);
                font-size: 22px;
                color: #efdaff;
            }

tips:彈性盒子display:flex布局+margin:auto可實(shí)現(xiàn)完美居中。

JS代碼

        <script>
            var counter = document.getElementById("demo").innerHTML;
            function add() {
                 counter++;
                 document.getElementById("demo").innerHTML = counter;
            }

            function sub() {
                if(counter == 0) {
                   counter = 0;
                } else {
                    counter--;
                    document.getElementById("demo").innerHTML = counter;
                }
            }

            function zero() {
                counter = 0;
                document.getElementById("demo").innerHTML = counter;
            }
        </script>

以上代碼即可實(shí)現(xiàn)效果。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容