186 lines
4.3 KiB
HTML
186 lines
4.3 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="ru">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
<title>Калькулятор</title>
|
||
<style>
|
||
:root {
|
||
--ink: #0F1218;
|
||
--cyan: #00E5FF;
|
||
--cyan-50: #E8FCFF;
|
||
--white: #fff;
|
||
--gray-500: #5B6573;
|
||
--gray-100: #F2F4F7;
|
||
}
|
||
* { box-sizing: border-box; margin: 0; padding: 0 }
|
||
body {
|
||
font: 17px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif;
|
||
color: var(--ink);
|
||
background: var(--gray-100);
|
||
min-height: 100vh;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
.calc {
|
||
background: var(--white);
|
||
border-radius: 16px;
|
||
padding: 24px;
|
||
width: 320px;
|
||
box-shadow: 0 4px 24px rgba(15,18,24,0.12);
|
||
}
|
||
.display {
|
||
background: var(--gray-100);
|
||
border-radius: 12px;
|
||
padding: 20px;
|
||
margin-bottom: 20px;
|
||
text-align: right;
|
||
font-size: 36px;
|
||
font-weight: 700;
|
||
color: var(--ink);
|
||
line-height: 1.1;
|
||
min-height: 80px;
|
||
display: flex;
|
||
align-items: flex-end;
|
||
justify-content: flex-end;
|
||
word-break: break-all;
|
||
}
|
||
.buttons {
|
||
display: grid;
|
||
grid-template-columns: repeat(4, 1fr);
|
||
gap: 10px;
|
||
}
|
||
.btn {
|
||
border: none;
|
||
border-radius: 10px;
|
||
padding: 18px 0;
|
||
font-size: 20px;
|
||
font-weight: 600;
|
||
cursor: pointer;
|
||
transition: background 0.15s, transform 0.1s;
|
||
font-family: inherit;
|
||
}
|
||
.btn:active {
|
||
transform: scale(0.95);
|
||
}
|
||
.btn-num {
|
||
background: var(--gray-100);
|
||
color: var(--ink);
|
||
}
|
||
.btn-num:hover { background: #dde1e8 }
|
||
.btn-op {
|
||
background: var(--cyan-50);
|
||
color: var(--ink);
|
||
}
|
||
.btn-op:hover { background: #d0f5fa }
|
||
.btn-op.active {
|
||
background: var(--cyan);
|
||
}
|
||
.btn-eq {
|
||
background: var(--cyan);
|
||
color: var(--ink);
|
||
}
|
||
.btn-eq:hover { background: #1be5ff }
|
||
.btn-clear {
|
||
background: var(--gray-500);
|
||
color: var(--white);
|
||
}
|
||
.btn-clear:hover { background: #6b7482 }
|
||
.btn-zero {
|
||
grid-column: span 2;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
|
||
<div class="calc">
|
||
<div class="display" id="display">0</div>
|
||
<div class="buttons">
|
||
<button class="btn btn-num" data-num="7">7</button>
|
||
<button class="btn btn-num" data-num="8">8</button>
|
||
<button class="btn btn-num" data-num="9">9</button>
|
||
<button class="btn btn-op" data-op="−">−</button>
|
||
|
||
<button class="btn btn-num" data-num="4">4</button>
|
||
<button class="btn btn-num" data-num="5">5</button>
|
||
<button class="btn btn-num" data-num="6">6</button>
|
||
<button class="btn btn-op" data-op="+">+</button>
|
||
|
||
<button class="btn btn-num" data-num="1">1</button>
|
||
<button class="btn btn-num" data-num="2">2</button>
|
||
<button class="btn btn-num" data-num="3">3</button>
|
||
<button class="btn btn-clear" id="clear">C</button>
|
||
|
||
<button class="btn btn-num btn-zero" data-num="0">0</button>
|
||
<button class="btn btn-num" data-num=".">.</button>
|
||
<button class="btn btn-eq" id="eq">=</button>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
const display = document.getElementById('display');
|
||
let current = '0';
|
||
let previous = '';
|
||
let operation = null;
|
||
let resetDisplay = false;
|
||
|
||
function updateDisplay(val) {
|
||
current = val;
|
||
display.textContent = val || '0';
|
||
}
|
||
|
||
document.querySelectorAll('[data-num]').forEach(btn => {
|
||
btn.addEventListener('click', () => {
|
||
const num = btn.dataset.num;
|
||
if (current === '0' && num !== '.') current = '';
|
||
if (resetDisplay) { current = ''; resetDisplay = false; }
|
||
if (num === '.' && current.includes('.')) return;
|
||
current += num;
|
||
updateDisplay(current);
|
||
});
|
||
});
|
||
|
||
document.querySelectorAll('[data-op]').forEach(btn => {
|
||
btn.addEventListener('click', () => {
|
||
const op = btn.dataset.op;
|
||
if (previous !== '' && !resetDisplay) compute();
|
||
previous = current;
|
||
operation = op;
|
||
resetDisplay = true;
|
||
});
|
||
});
|
||
|
||
document.getElementById('clear').addEventListener('click', () => {
|
||
current = '0';
|
||
previous = '';
|
||
operation = null;
|
||
resetDisplay = false;
|
||
updateDisplay('0');
|
||
});
|
||
|
||
document.getElementById('eq').addEventListener('click', () => {
|
||
if (operation && previous !== '') {
|
||
compute();
|
||
previous = '';
|
||
operation = null;
|
||
}
|
||
});
|
||
|
||
function compute() {
|
||
const a = parseFloat(previous);
|
||
const b = parseFloat(current);
|
||
if (isNaN(a) || isNaN(b)) return;
|
||
let result;
|
||
if (operation === '+') result = a + b;
|
||
else if (operation === '−') result = a - b;
|
||
else return;
|
||
current = String(Math.round(result * 1e10) / 1e10);
|
||
updateDisplay(current);
|
||
resetDisplay = true;
|
||
}
|
||
</script>
|
||
|
||
</body>
|
||
</html>
|