Widget:Teste
Ir para navegação
Ir para pesquisar
<style>
/* Estilizando a calculadora */
#calc {
width: 220px;
border: 2px solid #444;
padding: 10px;
border-radius: 8px;
background-color: #f9f9f9;
margin: 10px auto;
font-family: Arial, sans-serif;
}
#display {
width: 100%;
height: 40px;
font-size: 18px;
text-align: right;
margin-bottom: 10px;
padding: 5px;
border: 1px solid #ccc;
border-radius: 4px;
}
.buttons {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.buttons button {
width: 48px;
height: 48px;
font-size: 16px;
margin-bottom: 5px;
cursor: pointer;
border: 1px solid #888;
border-radius: 4px;
background-color: #eee;
transition: background-color 0.2s ease;
}
.buttons button:hover {
background-color: #ddd;
}
/* Botão de limpar ocupa toda a linha */
.clear-btn {
width: 100%;
background-color: #f66;
color: #fff;
}
.clear-btn:hover {
background-color: #e55;
}
</style>
<input type="text" id="display" disabled />
<script>
// Adiciona o valor clicado ao display
function appendToDisplay(val) {
document.getElementById('display').value += val;
}
// Calcula o resultado usando eval() e trata possíveis erros
function computeResult() {
let display = document.getElementById('display');
try {
// Usamos eval() para realizar o cálculo
let result = eval(display.value);
display.value = result;
} catch (e) {
display.value = "Erro";
}
}
// Limpa o display
function clearDisplay() {
document.getElementById('display').value = ;
}
</script>