js、html、css制作计算器
时间: 2023-10-09 15:16:36 浏览: 69
好的,下面是一个简单的计算器的 HTML、CSS 和 JavaScript 代码示例:
HTML:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>计算器</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="calculator">
<div class="screen"></div>
<button class="button" value="AC">AC</button>
<button class="button" value="CE">CE</button>
<button class="button" value="%">%</button>
<button class="button operator" value="/">÷</button>
<button class="button" value="7">7</button>
<button class="button" value="8">8</button>
<button class="button" value="9">9</button>
<button class="button operator" value="*">×</button>
<button class="button" value="4">4</button>
<button class="button" value="5">5</button>
<button class="button" value="6">6</button>
<button class="button operator" value="-">-</button>
<button class="button" value="1">1</button>
<button class="button" value="2">2</button>
<button class="button" value="3">3</button>
<button class="button operator" value="+">+</button>
<button class="button" value="0">0</button>
<button class="button" value=".">.</button>
<button class="button operator" id="equals" value="=">=</button>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS:
```
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}
.calculator {
width: 300px;
margin: 50px auto;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #fff;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
}
.screen {
height: 50px;
margin-bottom: 10px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #fff;
font-size: 24px;
text-align: right;
}
.button {
width: 50px;
height: 50px;
margin: 5px;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #fff;
font-size: 18px;
font-weight: bold;
text-align: center;
cursor: pointer;
}
.operator {
background-color: #f2f2f2;
}
.button:hover {
background-color: #ccc;
color: #fff;
}
```
JavaScript:
```
const screen = document.querySelector('.screen');
const buttons = document.querySelectorAll('.button');
let input = '';
let result = null;
let operator = '';
buttons.forEach(button => {
button.addEventListener('click', () => {
const value = button.getAttribute('value');
if (value === 'AC') {
input = '';
result = null;
operator = '';
} else if (value === 'CE') {
input = input.slice(0, -1);
} else if (value === '+' || value === '-' || value === '*' || value === '/') {
operator = value;
result = parseFloat(input);
input = '';
} else if (value === '%') {
input = parseFloat(input) / 100;
} else if (value === '.') {
if (input.indexOf('.') === -1) {
input += '.';
}
} else if (value === '=') {
const num1 = result;
const num2 = parseFloat(input);
if (operator === '+') {
input = num1 + num2;
} else if (operator === '-') {
input = num1 - num2;
} else if (operator === '*') {
input = num1 * num2;
} else if (operator === '/') {
input = num1 / num2;
}
result = null;
operator = '';
} else {
input += value;
}
screen.textContent = input;
});
});
```
这是一个基本的计算器,你可以根据自己的需求进行修改和优化。
阅读全文