用js +css + html写一个Windows计算器
时间: 2023-03-19 08:25:00 浏览: 322
可以使用HTML、CSS和JavaScript来编写一个Windows计算器,但是需要一定的知识和技能。可以先使用HTML创建一个基本的网页框架,然后用CSS来调整样式,最后用JavaScript来添加计算器的功能。
相关问题
用js +css + html写一个Windows计算器,用代码实现
以下是一个简单的Windows计算器的示例,使用HTML、CSS和JavaScript编写:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Windows Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="calculator">
<div class="output">
<input type="text" id="result" readonly>
</div>
<div class="buttons">
<button onclick="clearResult()">C</button>
<button onclick="backspace()">←</button>
<button onclick="addToResult('(')">(</button>
<button onclick="addToResult(')')">)</button>
<button onclick="addToResult('7')">7</button>
<button onclick="addToResult('8')">8</button>
<button onclick="addToResult('9')">9</button>
<button onclick="addToResult('/')">÷</button>
<button onclick="addToResult('4')">4</button>
<button onclick="addToResult('5')">5</button>
<button onclick="addToResult('6')">6</button>
<button onclick="addToResult('*')">×</button>
<button onclick="addToResult('1')">1</button>
<button onclick="addToResult('2')">2</button>
<button onclick="addToResult('3')">3</button>
<button onclick="addToResult('-')">-</button>
<button onclick="addToResult('0')">0</button>
<button onclick="addToResult('.')">.</button>
<button onclick="calculate()">=</button>
<button onclick="addToResult('+')">+</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```css
body {
background-color: #ddd;
font-family: Arial, sans-serif;
font-size: 16px;
}
.calculator {
background-color: #f2f2f2;
border: 2px solid #999;
border-radius: 5px;
margin: 20px auto;
padding: 10px;
width: 300px;
}
.output {
background-color: #fff;
border: 1px solid #999;
border-radius: 5px;
margin-bottom: 10px;
padding: 5px;
}
.output input {
border: none;
font-size: 24px;
width: 100%;
}
.buttons {
display: grid;
grid-gap: 5px;
grid-template-columns: repeat(4, 1fr);
}
button {
background-color: #fff;
border: 1px solid #999;
border-radius: 5px;
font-size: 24px;
padding: 10px;
}
```
JavaScript代码:
```javascript
const result = document.getElementById('result');
function addToResult(value) {
result.value += value;
}
function clearResult() {
result.value = '';
}
function backspace() {
result.value = result.value.slice(0, -1);
}
function calculate() {
try {
result.value = eval(result.value);
} catch(error) {
result.value = 'Error';
}
}
```
在这个示例中,我们创建了一个包含两个主要部分的div元素。第一个部分包含一个只读文本输入框,它用于显示结果。第二个
使用简单的html+css+js写出一个计算器程序
以下是一个简单的计算器程序的HTML、CSS和JS代码:
HTML代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="calculator">
<div class="display"></div>
<button class="button" id="clear">AC</button>
<button class="button" id="sign">+/-</button>
<button class="button" id="percent">%</button>
<button class="button operator" id="divide">÷</button>
<button class="button" id="7">7</button>
<button class="button" id="8">8</button>
<button class="button" id="9">9</button>
<button class="button operator" id="multiply">×</button>
<button class="button" id="4">4</button>
<button class="button" id="5">5</button>
<button class="button" id="6">6</button>
<button class="button operator" id="subtract">-</button>
<button class="button" id="1">1</button>
<button class="button" id="2">2</button>
<button class="button" id="3">3</button>
<button class="button operator" id="add">+</button>
<button class="button zero" id="0">0</button>
<button class="button" id="decimal">.</button>
<button class="button operator" id="equals">=</button>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.calculator {
width: 300px;
margin: 50px auto;
background-color: #eee;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
padding: 20px;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
}
.display {
grid-column: 1 / -1;
background-color: #fff;
border-radius: 5px;
padding: 10px;
font-size: 24px;
text-align: right;
}
.button {
background-color: #ccc;
border: none;
border-radius: 5px;
padding: 10px;
font-size: 18px;
cursor: pointer;
transition: background-color 0.2s ease;
}
.button:hover {
background-color: #bbb;
}
.operator {
background-color: #ff9500;
color: #fff;
}
.zero {
grid-column: 1 / span 2;
}
#equals {
background-color: #ff9500;
color: #fff;
grid-row: 5 / span 2;
}
#clear {
background-color: #d9534f;
color: #fff;
}
#sign {
background-color: #f0ad4e;
color: #fff;
}
#percent {
background-color: #5bc0de;
color: #fff;
}
```
JS代码:
```js
let display = document.querySelector('.display');
let clear = document.querySelector('#clear');
let sign = document.querySelector('#sign');
let percent = document.querySelector('#percent');
let divide = document.querySelector('#divide');
let multiply = document.querySelector('#multiply');
let subtract = document.querySelector('#subtract');
let add = document.querySelector('#add');
let equals = document.querySelector('#equals');
let decimal = document.querySelector('#decimal');
let buttons = document.querySelectorAll('.button:not(#clear):not(#sign):not(#percent):not(#divide):not(#multiply):not(#subtract):not(#add):not(#equals):not(#decimal)');
let input = '';
let operator = '';
let result = '';
buttons.forEach(button => {
button.addEventListener('click', () => {
input += button.textContent;
display.textContent = input;
});
});
clear.addEventListener('click', () => {
input = '';
operator = '';
result = '';
display.textContent = '0';
});
sign.addEventListener('click', () => {
input = input * -1;
display.textContent = input;
});
percent.addEventListener('click', () => {
input = input / 100;
display.textContent = input;
});
divide.addEventListener('click', () => {
operator = '/';
result = input;
input = '';
});
multiply.addEventListener('click', () => {
operator = '*';
result = input;
input = '';
});
subtract.addEventListener('click', () => {
operator = '-';
result = input;
input = '';
});
add.addEventListener('click', () => {
operator = '+';
result = input;
input = '';
});
equals.addEventListener('click', () => {
switch(operator) {
case '/':
input = result / input;
break;
case '*':
input = result * input;
break;
case '-':
input = Number(result) - Number(input);
break;
case '+':
input = Number(result) + Number(input);
break;
}
display.textContent = input;
});
decimal.addEventListener('click', () => {
if (!input.includes('.')) {
input += '.';
display.textContent = input;
}
});
```
上述代码实现了一个简单的四则运算计算器,包含基本的功能按钮、数字按钮、小数点按钮、清空按钮、正负号按钮、百分号按钮等。用户可以通过点击按钮来输入数字和运算符,计算器可以实时显示用户输入的结果,并且支持实现四则运算。
阅读全文