JavaScript: 制作简单计算器

效果图如下:

简单计算器

下面来讲讲我的思路:
简单的说就是利用HTML与CSS制作出一个计算器的外观,然后通过js来实现用户与计算器之间的交互,进而实现简单的计算。

对于HTML:
According to semantic HTML, I use <bottom>to represent every button, using <input> to display the result.

对于CSS:
I use gradual color to decorate my calculator, together with different color of button, special font-family of result, motivating the real calculator to the greatest extent.

对于JavaScript:
First, set the property of buttons — onclick().
Then make good use of the function of eval(), which can not only test whether the arithmetic expression is valid, but also deal with the expression and display the result.

下面是我的代码:

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Calculator</title>
        <link rel="stylesheet" type="text/css" href="style.css">
        <script type="text/javascript" src="calculate.js"></script>
    </head>
    <body>
        <h1 id="title">小威威de计算器</h1>
        <div id="calculator_container">
            <div id="calculator">
                <input type="result" disabled="disabled" id="result">
                <div id="first_row">
                    <button id="seven" class="button">7</button>
                    <button id="eight" class="button">8</button>
                    <button id="nine" class="button">9</button>
                    <button id="divide" class="button">/</button>
                </div>
                <div id="second_row">
                    <button id="four" class="button">4</button>
                    <button id="five" class="button">5</button>
                    <button id="six" class="button">6</button>
                    <button id="multiply" class="button">*</button>
                </div>
                <div id="third_row">
                    <button id="one" class="button">1</button>
                    <button id="two" class="button">2</button>
                    <button id="three" class="button">3</button>
                    <button id="subtract" class="button">-</button>
                </div>
                <div id="fourth_row">
                    <button id="zero" class="button">0</button>
                    <button id="point" class="button">.</button>
                    <button id="delete_one" class="button"></button>
                    <button id="plus" class="button">+</button>
                </div>
                <div id="fifth_row">
                    <button id="left_bracket" class="button">(</button>
                    <button id="right_bracket" class="button">)</button>
                    <button id="delete_all" class="button">CE</button>
                    <button id="equal" class="button">=</button>
                </div>
            </div>
        </div>
    </body>
</html>

CSS:

/*
    project: My calculator;
    time: Wed Oct5 8:31PM;
    Feature: 1.gradual change of background-color;
             2.protrusion of button togrther with calculator itself;
             3.different color of button, using opacity;
             4.using new font-family of displaying, let it be more similar to the real one;
             5.setting the background with a comfortable image;
             6.it can test whether Arithmetic Expressions is valid, if not, alert will occur;
             7.it can deal with the big number using science notation;
             8.it can use the result of the last calculation and continue the next one;
             9.when alert occurs, you can continue to change the expression instead of inputing again;
             10.it owns special color of the result.
*/

/* Large layout location */
html, body {
    width: 100%;
    height: 100%;
    margin: 0px;
    padding: 0px;
    overflow: hidden;
    background-image: url("background.jpg");
}

/* Define my own font-family */
@font-face {
    font-family: 'calculate_style';
    src:url("1.ttf");
}

#title {
    text-align: center;
    color: green;
    font-size: 44px;
    width: 400px;
    margin-left: auto;
    margin-right: auto;
    border-style: dotted;
}

#calculator {
    border:5px solid black;
    width: 317px;
    height: 417px;
    margin-left: auto;
    margin-right: auto;
    margin-top: 15px;
    background-color:#373737;
    /* Gradual color */
    background-image: -webkit-gradient(linear, center top, center bottom, from(rgb(43,43,43)), color-stop(50%, rgb(118,118,118)) ,to(rgb(43,43,43)));
}

#calculator_container {
    border:1px solid black;
    width:360px;
    height: 460px;
    margin-left: auto;
    margin-right: auto;
    box-shadow: -3px 6px 3px, -13px 5px 3px;
    background-color: #272727;
    /* Gradual color */
    background-image: -webkit-gradient(linear, center left, center right, from(rgb(43,43,43)), color-stop(50%, rgb(118,118,118)) ,to(rgb(43,43,43)));
}

#result {
    width: 294px;
    height: 48px;
    border: 3px solid gray;
    margin-left: 8px;
    margin-top: 8px;
    margin-bottom: 12px;
    background-color: #1D1D1D;
    text-align: right;
    color: #80CAAC;
    font-size: 30px;
    /* using my own font-family */
    font-family: calculate_style;
}

.button {
    width: 65px;
    height: 65px;
    font-size: 20px;
    color: white;
    margin-left:6px;
    border-radius: 35%;
    background-color: #1F1F1F;
    /* make the protrusion of botton  */
    box-shadow: -2px 3px #FFF, -4px 3px #AAA, -6px 6px #666, -8px 9px #000;
    opacity: 0.8;
}

#first_row,#second_row,#third_row,#fourth_row,#fifth_row {
    margin-left: 12px;
}

#equal {
    background-color:rgb(104,1,2);
}

#delete_all,#delete_one {
    background-color:rgb(104,1,2);
}

#plus,#subtract,#multiply,#divide {
    background-color: rgb(43,62,92);
}

JavaScript:

var display = ""; // store the result

/* response to users */
window.onload = function() {
    document.getElementById("zero").onclick=function() {
        display += "0";
        document.getElementById("result").value = display;
    }
    document.getElementById("one").onclick=function() {
        display += "1";
        document.getElementById("result").value = display
    }
    document.getElementById("two").onclick=function() {
        display += "2";
        document.getElementById("result").value = display;
    }
    document.getElementById("three").onclick=function() {
        display += "3";
        document.getElementById("result").value = display;
    }
    document.getElementById("four").onclick=function() {
        display += "4";
        document.getElementById("result").value = display;
    }
    document.getElementById("five").onclick=function() {
        display += "5";
        document.getElementById("result").value = display;
    }
    document.getElementById("six").onclick=function() {
        display += "6";
        document.getElementById("result").value = display;
    }
    document.getElementById("seven").onclick=function() {
        display += "7";
        document.getElementById("result").value = display;
    }
    document.getElementById("eight").onclick=function() {
        display += "8";
        document.getElementById("result").value = display;
    }
    document.getElementById("nine").onclick=function() {
        display += "9";
        document.getElementById("result").value = display;
    }
    document.getElementById("plus").onclick=function() {
        display += "+";
        document.getElementById("result").value = display;
    }
    document.getElementById("subtract").onclick=function() {
        display += "-";
        document.getElementById("result").value = display;
    }
    document.getElementById("multiply").onclick=function() {
        display += "*";
        document.getElementById("result").value = display;
    }
    document.getElementById("divide").onclick=function() {
        display += "/";
        document.getElementById("result").value = display;
    }
    document.getElementById("point").onclick=function() {
        display += ".";
        document.getElementById("result").value = display;
    }
    document.getElementById("delete_one").onclick=function() {
        display = display.substring(0, display.length-1);
        document.getElementById("result").value = display;
    }
    document.getElementById("delete_all").onclick=function() {
        display = " ";
        document.getElementById("result").value = display;
    }
    document.getElementById("left_bracket").onclick=function() {
        display += "(";
        document.getElementById("result").value = display;
    }
    document.getElementById("right_bracket").onclick=function() {
        display += ")";
        document.getElementById("result").value = display;
    }
    document.getElementById("equal").onclick=function() {
        /* using try...catch... with eval() to realize calculate */
        try {
            display = eval(display);
        }
        catch(exception) {
            alert("Invalid arithmetic expressions!");
        }
        document.getElementById("result").value = display;
    }
}

改进版:
简单计算器

代码详见github: linwh8/ModernWebPrograming


以上内容皆为本人观点,欢迎大家提出批评和指导,我们一起探讨!


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值