-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
183 lines (159 loc) · 4.49 KB
/
index.html
File metadata and controls
183 lines (159 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/styles.css">
<title>Calculator</title>
<style>
.calculator {
margin: 50px auto;
padding: 18px;
width: 300px;
height: 500px;
background-color: #000000;
border-radius: 20px;
color: #FFFFFF;
font-family: Arial;
box-shadow: 4px 4px 4px #3D3E42
}
.screen {
text-align: right;
user-select: none;
line-height: 10px;
font-size: 60px;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 70px);
grid-template-rows: repeat(4, 70px);
grid-gap: 7px;
justify-items: center;
}
.btn {
width: 60px;
height: 60px;
background-color:#3D3E42;
border-radius: 50%;
text-align: center;
line-height: 60px;
user-select: none;
font-size: 1.5rem;
}
.btn:hover {
filter: brightness(120%)
}
.btn:active {
filter: brightness(50%)
}
.bg-orange {
background-color: #F37D0D
}
.bg-grey {
background-color: #B4B0AC
}
.zero {
grid-column: 1/3;
width:100%;
border-radius: 34px;
}
.header {
display: inline-block;
align-items: center;
justify-self: first baseline;
align-items: center;
}
</style>
</head>
<body>
<div id="decription" class="heading"></div>
<h1>iOS Calculator</h1>
<p>Made with only CSS, HTML5 and vanilla Javascript</p>
<p>© Steven Rugg 2024 http://github.com/stevenrugg/ioscalculator</p>
<a href="https://github.com/stevenrugg/ioscalculator">Back to Github</a>
</div>
<div class="calculator">
<div class="screen">
<p id="calcDisplay">0</p>
</div>
<div class="buttons">
<div id="acBtn"class="btn ac bg-grey">ac</div>
<div id="plusMinusBtn" class="btn plus/minus bg-grey">+/-</div>
<div id="percentBtn"class="btn percent bg-grey">%</div>
<div id="divisionBtn" class="btn division bg-orange">/</div>
<div id="sevenBtn" class="btn seven">7</div>
<div id="eightBtn" class="btn eight">8</div>
<div id="nineBtn" class="btn nine">9</div>
<div id="multiplyBtn" class="btn multiply bg-orange">*</div>
<div id="fourBtn" class="btn four">4</div>
<div id="fiveBtn" class="btn five">5</div>
<div id="sixBtn" class="btn six">6</div>
<div id="minusBtn" class="btn minus bg-orange">-</div>
<div id="oneBtn" class="btn one">1</div>
<div id="twoBtn" class="btn two">2</div>
<div id="threeBtn" class="btn three">3</div>
<div id="plusBtn" class="btn plus bg-orange">+</div>
<div id="zeroBtn" class="btn zero">0</div>
<div id="dotBtn" class="btn quote">.</div>
<div id="equalsBtn" class="btn equals bg-orange">=</div>
</div>
</div>
<script type=application/javascript>
// Get the display element
const display = document.getElementById('calcDisplay');
// Initialize the display value
let currentValue = '0';
// Add event listeners to each button
document.querySelectorAll('.btn').forEach((button) => {
button.addEventListener('click', (event) => {
const buttonText = event.target.textContent;
handleButtonPress(buttonText);
});
});
// Function to handle button presses
function handleButtonPress(buttonText) {
switch (buttonText) {
case 'ac':
// Clear the display
currentValue = '0';
break;
case '+/-':
// Toggle the sign of the current value
currentValue = currentValue * -1;
break;
case '%':
// Calculate the percentage of the current value
currentValue = currentValue / 100;
break;
case '=':
// Evaluate the expression and display the result
try {
currentValue = eval(currentValue);
} catch (error) {
currentValue = 'Error';
}
break;
case '+':
case '-':
case '*':
case '/':
// Append the operator to the current value
currentValue += buttonText;
break;
default:
// Append the digit to the current value
if (currentValue === '0') {
currentValue = buttonText;
} else {
currentValue += buttonText;
}
}
updateDisplay();
}
// Function to update the display
function updateDisplay() {
display.textContent = currentValue;
}
</script>
</body>
</html>