写一个计算器的web前端,简约风格 html
时间: 2023-06-26 21:05:51 浏览: 420
以下是一个简单的计算器前端代码,使用HTML和CSS实现了简约风格:
```html
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<meta charset="utf-8">
<style>
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
text-align: center;
}
#calculator {
width: 300px;
margin: 0 auto;
background-color: #fff;
border-radius: 10px;
padding: 10px;
box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.3);
}
#result {
height: 50px;
font-size: 24px;
text-align: right;
padding: 10px;
border: none;
background-color: #f2f2f2;
border-radius: 5px;
margin-bottom: 10px;
}
.button {
display: inline-block;
width: 50px;
height: 50px;
font-size: 24px;
background-color: #e6e6e6;
border-radius: 5px;
margin: 5px;
cursor: pointer;
box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.2);
}
.button:hover {
background-color: #d9d9d9;
}
.button:active {
background-color: #ccc;
box-shadow: none;
}
</style>
</head>
<body>
<div id="calculator">
<input type="text" id="result" readonly>
<div>
<button class="button" onclick="append('7')">7</button>
<button class="button" onclick="append('8')">8</button>
<button class="button" onclick="append('9')">9</button>
<button class="button" onclick="append('/')">/</button>
</div>
<div>
<button class="button" onclick="append('4')">4</button>
<button class="button" onclick="append('5')">5</button>
<button class="button" onclick="append('6')">6</button>
<button class="button" onclick="append('*')">*</button>
</div>
<div>
<button class="button" onclick="append('1')">1</button>
<button class="button" onclick="append('2')">2</button>
<button class="button" onclick="append('3')">3</button>
<button class="button" onclick="append('-')">-</button>
</div>
<div>
<button class="button" onclick="append('0')">0</button>
<button class="button" onclick="append('.')">.</button>
<button class="button" onclick="calculate()">=</button>
<button class="button" onclick="append('+')">+</button>
</div>
<div>
<button class="button" onclick="clearResult()">Clear</button>
</div>
</div>
<script>
// 获取结果输入框元素
var result = document.getElementById('result');
// 在结果输入框中添加字符
function append(char) {
result.value += char;
}
// 计算结果
function calculate() {
try {
result.value = eval(result.value);
} catch (e) {
result.value = '';
}
}
// 清除结果输入框
function clearResult() {
result.value = '';
}
</script>
</body>
</html>
```
该计算器具有简洁的界面,包含数字、小数点、加减乘除等按钮,可进行基本的四则运算。
阅读全文