Skip to content

Commit 6db4bc1

Browse files
committed
原生js
1 parent 4eebb6e commit 6db4bc1

5 files changed

Lines changed: 249 additions & 0 deletions

File tree

32.5 KB
Binary file not shown.
File renamed without changes.

‎js计算器/css/style.css‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
body{
2+
font-size: 12px;
3+
font-family:Arial, Georgia, "Times New Roman", Times, serif;
4+
color: #555;
5+
text-align: center;
6+
background-color: #e2e2e2;
7+
}
8+
h6{
9+
margin:0;
10+
font-size:12px;
11+
}
12+
#calculator {
13+
width: 240px;
14+
height: auto;
15+
overflow:hidden;
16+
margin:10px auto;
17+
border:#fff 1px solid;
18+
padding-bottom:10px;
19+
background-color:#f2f2f2;
20+
}
21+
#calculator div {
22+
clear:both;
23+
}
24+
#calculator ul {
25+
padding:0;
26+
margin: 5px 14px;
27+
border: 1px solid #FFFFFF;
28+
height:auto;
29+
overflow: hidden;
30+
}
31+
#calculator li {
32+
list-style: none;
33+
float: left;
34+
width: 32px;
35+
height: 32px;
36+
margin: 5px;
37+
display: inline;
38+
line-height: 32px;
39+
font-size: 14px;
40+
background-color: #eaeaea;
41+
}
42+
#calculator li.tool{
43+
background-color:#e2e2e2;
44+
}
45+
#calculator li:hover{
46+
background-color:#f9f9f9;
47+
cursor:pointer;
48+
}
49+
#calculator li:active{
50+
background-color:#fc0;
51+
cursor:pointer;
52+
}
53+
#calculator li.tool:active{
54+
background-color:#d8e8ff;
55+
cursor:pointer;
56+
}
57+
#calcu-head {
58+
text-align:left;
59+
padding:10px 15px 5px;
60+
}
61+
span.imyeah {
62+
float:right;
63+
color:#ccc;
64+
}
65+
span.imyeah a{
66+
color:#ccc;
67+
}
68+
.screen{
69+
width:200px;
70+
height:24px;
71+
line-height:24px;
72+
padding:4px;
73+
border:#e6e6e6 1px solid;
74+
border-bottom:#f2f2f2 1px solid;
75+
border-right:#f2f2f2 1px solid;
76+
margin:10px auto;
77+
direction:ltr;
78+
text-align:right;
79+
font-size:16px;
80+
color:#999;
81+
}
82+
#calcu-foot{
83+
text-align:left;
84+
padding:10px 15px 5px;
85+
height:auto;
86+
overflow:hidden;
87+
}
88+
span#note{
89+
float:left;
90+
width:210px;
91+
height:auto;
92+
overflow:hidden;
93+
color:red;
94+
}
95+
span.welcome{
96+
clear:both;
97+
color:#999;
98+
}
99+
span.welcome a{
100+
float:right;
101+
color:#999;
102+
}

‎js计算器/index.html‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>计算器</title>
6+
<link rel="stylesheet" href="css/style.css">
7+
</head>
8+
<body>
9+
<div id="calculator">
10+
<div id="calcu-head">
11+
<h6>简单的计算器</h6>
12+
</div>
13+
<form name="calculator" action="" method="get">
14+
<div id="calcu-screen">
15+
<input type="text" name="numScreen" class="screen" value="0" onfocus="this.blur()">
16+
</div>
17+
<div id="calcu_btn">
18+
<ul> <!--配置按钮-->
19+
<li onclick="command(7)">7</li>
20+
<li onclick="command(8)">8</li>
21+
<li onclick="command(9)">9</li>
22+
<li class="tool" onclick="del()">←</li>
23+
<li class="tool" onclick="clearscreen()">C</li>
24+
<li onclick="command(4)">4</li>
25+
<li onclick="command(5)">5</li>
26+
<li onclick="command(6)">6</li>
27+
<li class="tool" onclick="times()">×</li>
28+
<li class="tool" onclick="divide()">÷</li>
29+
<li onclick="command(1)">1</li>
30+
<li onclick="command(2)">2</li>
31+
<li onclick="command(3)">3</li>
32+
<li class="tool" onclick="plus()">+</li>
33+
<li class="tool" onclick="minus()">-</li>
34+
<li onclick="command(0)">0</li>
35+
<li onclick="dzero()">00</li>
36+
<li onclick="dot()">.</li>
37+
<li class="tool" onclick="persent()">%</li>
38+
<li class="tool" onclick="equal()">=</li>
39+
</ul>
40+
</div>
41+
<div id="calcu-foot">
42+
<span id="note"></span>
43+
</div>
44+
</form>
45+
</div>
46+
<script src="js/main.js"></script>
47+
</body>
48+
</html>

‎js计算器/js/main.js‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Created by NewRed on 2017/8/26.
3+
*/
4+
var num=0,result=0,numshow="0";
5+
var operate=0; //判断输入状态的标志
6+
var calcul=0; //判断计算状态的标志
7+
var quit=0; //防止重复按键的标志
8+
function command(num){
9+
var str=String(document.calculator.numScreen.value); //获得当前显示数据
10+
str=(str!="0") ? ((operate==0) ? str : "") : ""; //如果当前值不是"0",且状态为0,则返回当前值,否则返回空值;
11+
str=str + String(num); //给当前值追加字符
12+
document.calculator.numScreen.value=str; //刷新显示
13+
operate=0; //重置输入状态
14+
quit=0; //重置防止重复按键的标志
15+
}
16+
function dzero(){
17+
var str=String(document.calculator.numScreen.value);
18+
str=(str!="0") ? ((operate==0) ? str + "00" : "0") : "0"; //如果当前值不是"0",且状态为0,则返回当str+"00",否则返回"0";
19+
document.calculator.numScreen.value=str;
20+
operate=0;
21+
}
22+
function dot(){
23+
var str=String(document.calculator.numScreen.value);
24+
str=(str!="0") ? ((operate==0) ? str : "0") : "0"; //如果当前值不是"0",且状态为0,则返回当前值,否则返回"0";
25+
for(i=0; i<=str.length;i++){ //判断是否已经有一个点号
26+
if(str.substr(i,1)==".") return false; //如果有则不再插入
27+
}
28+
str=str + ".";
29+
document.calculator.numScreen.value=str;
30+
operate=0;
31+
}
32+
function del(){ //退格
33+
var str=String(document.calculator.numScreen.value);
34+
str=(str!="0") ? str : "";
35+
str=str.substr(0,str.length-1);
36+
str=(str!="") ? str : "0";
37+
document.calculator.numScreen.value=str;
38+
}
39+
function clearscreen(){ //清除数据
40+
num=0;
41+
result=0;
42+
numshow="0";
43+
document.calculator.numScreen.value="0";
44+
}
45+
function plus(){ //加法
46+
calculate(); //调用计算函数
47+
operate=1; //更改输入状态
48+
calcul=1; //更改计算状态为加
49+
}
50+
function minus(){ //减法
51+
calculate();
52+
operate=1;
53+
calcul=2;
54+
}
55+
function times(){ //乘法
56+
calculate();
57+
operate=1;
58+
calcul=3;
59+
}
60+
function divide(){ //除法
61+
calculate();
62+
operate=1;
63+
calcul=4;
64+
}
65+
function persent(){ //求余
66+
calculate();
67+
operate=1;
68+
calcul=5;
69+
}
70+
function equal(){
71+
calculate(); //等于
72+
operate=1;
73+
num=0;
74+
result=0;
75+
numshow="0";
76+
}
77+
//
78+
function calculate(){
79+
numshow=Number(document.calculator.numScreen.value);
80+
if(num!=0 && quit!=1){ //判断前一个运算数是否为零以及防重复按键的状态
81+
switch(calcul){ //判断要输入状态
82+
case 1:result=num+numshow;break; //计算"+"
83+
case 2:result=num-numshow;break; //计算"-"
84+
case 3:result=num*numshow;break;
85+
case 4:if(numshow!=0){result=num/numshow;}else{document.getElementById("note").innerHTML="被除数不能为零!"; setTimeout(clearnote,4000)} break;
86+
case 5:result=num%numshow;break;
87+
}
88+
quit=1; //避免重复按键
89+
}
90+
else{
91+
result=numshow;
92+
}
93+
numshow=String(result);
94+
document.calculator.numScreen.value=numshow;
95+
num=result; //存储当前值
96+
}
97+
function clearnote(){ //清空提示
98+
document.getElementById("note").innerHTML="";
99+
}

0 commit comments

Comments
 (0)