Uma calculadora simples é um excelente projeto inicial para praticar os fundamentos de HTML, CSS e JavaScript. Este tutorial é dividido em três partes: estrutura (HTML), estilo (CSS) e funcionalidade (JavaScript).
1. Estrutura HTML
Crie um arquivo chamado index.html
e copie o código abaixo. Ele define a estrutura básica da calculadora.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculadora Simples</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="calculator">
<input type="text" id="display" readonly>
<div class="buttons">
<button onclick="appendValue('7')">7</button>
<button onclick="appendValue('8')">8</button>
<button onclick="appendValue('9')">9</button>
<button onclick="clearDisplay()">C</button>
<button onclick="appendValue('4')">4</button>
<button onclick="appendValue('5')">5</button>
<button onclick="appendValue('6')">6</button>
<button onclick="appendValue('+')">+</button>
<button onclick="appendValue('1')">1</button>
<button onclick="appendValue('2')">2</button>
<button onclick="appendValue('3')">3</button>
<button onclick="appendValue('-')">-</button>
<button onclick="appendValue('0')">0</button>
<button onclick="appendValue('.')">.</button>
<button onclick="calculate()">=</button>
<button onclick="appendValue('*')">×</button>
<button onclick="appendValue('/')">÷</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
2. Estilizando com CSS
Crie um arquivo chamado styles.css
para aplicar estilos à sua calculadora.
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.calculator {
background: #fff;
border-radius: 10px;
padding: 20px;
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
#display {
width: 100%;
height: 50px;
margin-bottom: 10px;
font-size: 18px;
text-align: right;
padding: 5px;
border: 1px solid #ddd;
border-radius: 5px;
}
.buttons button {
width: 22%;
height: 50px;
margin: 1%;
font-size: 18px;
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 5px;
cursor: pointer;
}
.buttons button:hover {
background-color: #e0e0e0;
}
3. Adicionando Funcionalidade com JavaScript
Crie um arquivo chamado script.js
para adicionar a lógica da calculadora.
function appendValue(value) {
const display = document.getElementById('display');
display.value += value;
}
function clearDisplay() {
document.getElementById('display').value = '';
}
function calculate() {
const display = document.getElementById('display');
try {
display.value = eval(display.value); // Calcula a expressão
} catch (error) {
display.value = 'Erro'; // Exibe erro caso a expressão seja inválida
}
}
Como Configurar o Projeto
- Crie uma pasta para o projeto.
- Dentro da pasta, salve os três arquivos:
index.html
,styles.css
escript.js
. - Abra o arquivo
index.html
no navegador para testar a calculadora.
Dicas para Melhorar
- Substitua
eval()
por uma biblioteca matemática mais segura para projetos mais complexos. - Adicione botões avançados, como
sqrt
ou%
. - Personalize o design para torná-lo mais atraente.
Clique aqui e veja essa calculadora funcionando online!