那么
先上效果
效果图
如图所示这就是我们的成品样子
自己写出来的计算器好在哪呢,诶,问的好!
优点:简洁、显示清楚……算了编不下去了
缺点:没有! 我看谁敢说不好
测试结果:
谷歌浏览器 完美运行
火狐浏览器 完美运行
Microsoft Edge 浏览器 完美运行
其他浏览器 未测试
功能实现 : 整数 正负数 的 加减乘除
没问题的话 那让我们进入代码部分!
--------------------------------------------------分割线----------------------------------------------------
计算器的主体是简单的div和form组成
写好input按钮以确保不会缺少效果,然后给每一个按钮依次起名以便接下来使用
<div class="center">
<h1>计算器</h1>
<form name="calculator">
<input type="button" id="clear" class="btn other" onclick="qc()">
<input type="text" id="display">
<br>
<input type="button" class="btn number" value="7" onclick="a(this.value)">
<input type="button" class="btn number" value="8" onclick="a(this.value)">
<input type="button" class="btn number" value="9" onclick="a(this.value)">
<input type="button" class="btn operator" value="+" onclick="a(this.value)">
<br>
<input type="button" class="btn number" value="4" onclick="a(this.value)">
<input type="button" class="btn number" value="5" onclick="a(this.value)">
<input type="button" class="btn number" value="6" onclick="a(this.value)">
<input type="button" class="btn operator" value="*" onclick="a(this.value)">
<br>
<input type="button" class="btn number" value="1" onclick="a(this.value)">
<input type="button" class="btn number" value="2" onclick="a(this.value)">
<input type="button" class="btn number" value="3" onclick="a(this.value)">
<input type="button" class="btn operator" value="-" onclick="a(this.value)">
<br>
<input type="button" class="btn number" value="0" onclick="a(this.value)">
<input type="button" class="btn operator" value="." onclick="a(this.value)">
<input type="button" class="btn operator" value="/" onclick="a(this.value)">
<input type="button" class="btn other" value="=" onclick="deng()" >
</form>
</div>
然后是js部分
添加上一丢丢的自定义函数给予效果
<script type="text/javascript">
function qc(){
document.getElementById('display').value = "";
}
function a(value){
document.getElementById('display').value += value;
}
function deng(){
var value = document.getElementById('display').value;
document.getElementById('display').value = eval(value);
}
</script>
然后!就到了决定美观不美观的地方了,也就是css渲染部分
/* 涧 */
* {
border: none;
font-family: 'Open Sans', sans-serif;
margin: 0;
padding: 0;
}
.center {
background-color: #fff;
border-radius: 50%;
height: 600px;
margin: auto;
width: 600px;
}
h1 {
color: #495678;
font-size: 30px;
margin-top: 20px;
padding-top: 50px;
display: block;
text-align: center;
text-decoration: none;
}
form {
background-color: #495678;
box-shadow: 4px 4px #3d4a65;
margin: 40px auto;
padding: 40px 0 30px 40px;
width: 280px;
}
.btn {
cursor: pointer;
font-size: 20px;
height: 45px;
margin: 5px 0 5px 10px;
width: 45px;
}
.btn:first-child {
margin: 5px 0 5px 10px;
}
.btn, #display, form {
border-radius: 25px;
}
#display {
background-color: #98d1dc;
box-shadow: inset 6px 6px 0px #3facc0;
color: #dededc;
font-size: 20px;
height: 47px;
text-align: right;
width: 165px;
padding-right: 10px;
margin-left: 10px;
}
.number {
background-color: #72778b;
box-shadow: 0 5px #5f6680;
color: #dededc;
}
.number:active {
box-shadow: 0 2px #5f6680;
-webkit-transform: translateY(2px);
-ms-transform: translateY(2px);
-moz-tranform: translateY(2px);
transform: translateY(2px);
}
.operator {
background-color: #dededc;
box-shadow: 0 5px #bebebe;
color: #72778b;
}
.operator:active {
box-shadow: 0 2px #bebebe;
-webkit-transform: translateY(2px);
-ms-transform: translateY(2px);
-moz-tranform: translateY(2px);
transform: translateY(2px);
}
.other {
background-color: #e3844c;
box-shadow: 0 5px #e76a3d;
color: #dededc;
}
.other:active {
box-shadow: 0 2px #e76a3d;
-webkit-transform: translateY(2px);
-ms-transform: translateY(2px);
-moz-tranform: translateY(2px);
transform: translateY(2px);
}
铛铛!如此一顿操作我们的计算器就写好了! 肥肠简单 , 肥肠简约!
本来也想加点注释 但我想了想东西太少了没啥必要,那就这样了 其实还是懒