简易的 iPhone 计算器-JS、CSS、HTML

在这里插入图片描述

HTML+CSS 布局

 如下图:
  1. 最外层使用div然后margin:auto;水平居中
  2. 显示用户框我用的也是一个div 属性:text-align:right;文本右对齐
  3. table表格:5行4列 加上 border-radius圆角属性
    在这里插入图片描述
 <style>
        .panel {
            width: 270px;
            height: 400px;
            margin: auto;
            background-color: black;
            overflow: hidden;
        }

        .panel>div {
            height: 70px;
            width: 100%;
        }

        table {
            width: 270px;
            height: 300px;
            margin: auto;
            text-align: center;
        }

        table tr td {
            width: 20%;
            border-radius: 50%;
            cursor: pointer;
        }

        table tr:nth-last-child(1) td:nth-child(1){
            border-radius: 140px;
        }

        table tr:nth-child(1) td {
            background-color: #7c7c7c;
            color: #281622;
        }

        table tr td:nth-last-child(1) {
            background-color: #fc8e39;
            color: #fff;
        }

        table tr td {
            background-color: #2c2c2c;
            color: #fff;
        }

        .panel>div {
            color: #fff;
            font-size: 38px;
            margin-top: 30px;
            text-align: right;
        }
    </style>
    <body>
    <div class="panel">
        <div id="show"></div>
        <table border="0" cellspacing=8>
            <tr>
                <td οnclick="del()">del</td>
                <td οnclick="c()">C</td>
                <td οnclick="number(this)">%</td>
                <td οnclick="number(this)">/</td>
            </tr>
            <tr>
                <td οnclick="number(this)">7</td>
                <td οnclick="number(this)">8</td>
                <td οnclick="number(this)">9</td>
                <td οnclick="number(this)">*</td>
            </tr>
            <tr>
                <td οnclick="number(this)">4</td>
                <td οnclick="number(this)">5</td>
                <td οnclick="number(this)">6</td>
                <td οnclick="number(this)">-</td>
            </tr>
            <tr>
                <td οnclick="number(this)">1</td>
                <td οnclick="number(this)">2</td>
                <td οnclick="number(this)">3</td>
                <td οnclick="number(this)">+</td>
            </tr>
            <tr>
                <td colspan="2" οnclick="number(this)">0</td>
                <td οnclick="number(this)">.</td>
                <td οnclick="calculate()">=</td>
            </tr>
        </table>
    </div>
    </body>

JS 代码

由于JS代码没有多少且有注释,这里就不详细介绍了。

<script>
        // 获取显示框
        var show = document.getElementById('show');

        // 获取用户输入的数字,并显示
        function number(a) {
            show.innerText += a.innerText;
        }

        // 运算函数
        // 思路:1.判断显示框是否为空
        //      2.使用 eval(); 输出用户输入的算数
        function calculate() {
            show.innerText = show.innerText == "" ? "" : eval(show.innerText);
        }

        // 删除全部 del 函数
        function del() {
            show.innerText = "";
        }
        // 删除最后一位
        function c() {
            show.innerText = show.innerText.slice(0, show.innerText.length - 1);
        }
    </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值