AI 生成: 一个简洁美观的网页计算器,支持加减乘除和清除功能,使用纯 HTML/CSS/JS 实现。
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
# Simple Calculator Web
|
||||
|
||||
一个简洁美观的网页计算器,支持加、减、乘、除和清除功能。
|
||||
|
||||
## 功能特性
|
||||
|
||||
- ➕ 加法
|
||||
- ➖ 减法
|
||||
- ✖️ 乘法
|
||||
- ➗ 除法(自动处理除以零错误)
|
||||
- 🧹 清除(C 键重置所有状态)
|
||||
- 🎨 现代化渐变背景与圆角设计
|
||||
- 📱 响应式布局,适配移动端
|
||||
- ⚡ 纯原生实现,无需任何依赖
|
||||
|
||||
## 目录结构
|
||||
|
||||
```
|
||||
simple-calculator-web/
|
||||
├── index.html # 页面结构
|
||||
├── style.css # 样式表
|
||||
├── script.js # 计算逻辑
|
||||
└── README.md # 说明文档
|
||||
```
|
||||
|
||||
## 使用方法
|
||||
|
||||
1. 下载或克隆本项目到本地。
|
||||
2. 直接用浏览器打开 `index.html` 即可使用。
|
||||
3. 或者使用任意静态服务器托管:
|
||||
|
||||
```bash
|
||||
# 使用 Python 内置服务器
|
||||
python -m http.server 8000
|
||||
|
||||
# 或使用 Node.js 的 http-server
|
||||
npx http-server
|
||||
```
|
||||
|
||||
然后在浏览器访问 `http://localhost:8000`。
|
||||
|
||||
## 操作说明
|
||||
|
||||
| 按键 | 功能 |
|
||||
|------|------|
|
||||
| 0-9 | 输入数字 |
|
||||
| + | 加法 |
|
||||
| − | 减法 |
|
||||
| × | 乘法 |
|
||||
| ÷ | 除法 |
|
||||
| = | 计算结果 |
|
||||
| C | 清除全部 |
|
||||
|
||||
## 技术栈
|
||||
|
||||
- HTML5
|
||||
- CSS3(Grid 布局 + 渐变 + 过渡动画)
|
||||
- 原生 JavaScript(ES6+)
|
||||
|
||||
## 浏览器兼容性
|
||||
|
||||
支持所有现代浏览器(Chrome、Firefox、Safari、Edge)。
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>简单计算器</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="calculator">
|
||||
<!-- 显示屏 -->
|
||||
<input type="text" id="display" class="display" readonly value="0">
|
||||
|
||||
<!-- 按键区域 -->
|
||||
<div class="keys">
|
||||
<!-- 第一行:清除 + 除法 -->
|
||||
<button class="key clear" data-action="clear">C</button>
|
||||
<button class="key operator" data-action="divide">÷</button>
|
||||
|
||||
<!-- 数字 7 8 9 和 乘法 -->
|
||||
<button class="key" data-number="7">7</button>
|
||||
<button class="key" data-number="8">8</button>
|
||||
<button class="key" data-number="9">9</button>
|
||||
<button class="key operator" data-action="multiply">×</button>
|
||||
|
||||
<!-- 数字 4 5 6 和 减法 -->
|
||||
<button class="key" data-number="4">4</button>
|
||||
<button class="key" data-number="5">5</button>
|
||||
<button class="key" data-number="6">6</button>
|
||||
<button class="key operator" data-action="subtract">−</button>
|
||||
|
||||
<!-- 数字 1 2 3 和 加法 -->
|
||||
<button class="key" data-number="1">1</button>
|
||||
<button class="key" data-number="2">2</button>
|
||||
<button class="key" data-number="3">3</button>
|
||||
<button class="key operator" data-action="add">+</button>
|
||||
|
||||
<!-- 数字 0 和 等号 -->
|
||||
<button class="key zero" data-number="0">0</button>
|
||||
<button class="key equals" data-action="equals">=</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,122 @@
|
||||
// ===== 计算器状态 =====
|
||||
let currentInput = '0'; // 当前显示的数字
|
||||
let firstOperand = null; // 第一个操作数
|
||||
let operator = null; // 当前运算符
|
||||
let waitingForSecond = false; // 是否等待输入第二个操作数
|
||||
|
||||
// 获取显示屏元素
|
||||
const display = document.getElementById('display');
|
||||
|
||||
/**
|
||||
* 更新显示屏内容
|
||||
*/
|
||||
function updateDisplay() {
|
||||
display.value = currentInput;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输入数字
|
||||
* @param {string} num - 输入的数字字符
|
||||
*/
|
||||
function inputNumber(num) {
|
||||
if (waitingForSecond) {
|
||||
// 如果正在等待第二个操作数,则替换显示
|
||||
currentInput = num;
|
||||
waitingForSecond = false;
|
||||
} else {
|
||||
// 如果当前显示为 '0',直接替换;否则追加
|
||||
currentInput = currentInput === '0' ? num : currentInput + num;
|
||||
}
|
||||
updateDisplay();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理运算符
|
||||
* @param {string} op - 运算符类型: add, subtract, multiply, divide
|
||||
*/
|
||||
function handleOperator(op) {
|
||||
const inputValue = parseFloat(currentInput);
|
||||
|
||||
// 如果已有运算符且不在等待第二个操作数,先计算之前的结果
|
||||
if (operator !== null && !waitingForSecond) {
|
||||
const result = calculate(firstOperand, inputValue, operator);
|
||||
currentInput = String(result);
|
||||
firstOperand = result;
|
||||
updateDisplay();
|
||||
} else {
|
||||
firstOperand = inputValue;
|
||||
}
|
||||
|
||||
waitingForSecond = true;
|
||||
operator = op;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行计算
|
||||
* @param {number} a - 第一个操作数
|
||||
* @param {number} b - 第二个操作数
|
||||
* @param {string} op - 运算符
|
||||
* @returns {number} 计算结果
|
||||
*/
|
||||
function calculate(a, b, op) {
|
||||
switch (op) {
|
||||
case 'add': return a + b;
|
||||
case 'subtract': return a - b;
|
||||
case 'multiply': return a * b;
|
||||
case 'divide': return b === 0 ? '错误' : a / b;
|
||||
default: return b;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理等号
|
||||
*/
|
||||
function handleEquals() {
|
||||
if (operator === null || waitingForSecond) return;
|
||||
|
||||
const inputValue = parseFloat(currentInput);
|
||||
const result = calculate(firstOperand, inputValue, operator);
|
||||
|
||||
currentInput = String(result);
|
||||
firstOperand = null;
|
||||
operator = null;
|
||||
waitingForSecond = false;
|
||||
updateDisplay();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有状态
|
||||
*/
|
||||
function clearAll() {
|
||||
currentInput = '0';
|
||||
firstOperand = null;
|
||||
operator = null;
|
||||
waitingForSecond = false;
|
||||
updateDisplay();
|
||||
}
|
||||
|
||||
// ===== 事件绑定 =====
|
||||
document.querySelector('.keys').addEventListener('click', (e) => {
|
||||
const target = e.target;
|
||||
if (!target.classList.contains('key')) return;
|
||||
|
||||
// 数字键
|
||||
if (target.dataset.number !== undefined) {
|
||||
inputNumber(target.dataset.number);
|
||||
return;
|
||||
}
|
||||
|
||||
// 功能键
|
||||
const action = target.dataset.action;
|
||||
switch (action) {
|
||||
case 'clear': clearAll(); break;
|
||||
case 'add':
|
||||
case 'subtract':
|
||||
case 'multiply':
|
||||
case 'divide': handleOperator(action); break;
|
||||
case 'equals': handleEquals(); break;
|
||||
}
|
||||
});
|
||||
|
||||
// 初始化显示
|
||||
updateDisplay();
|
||||
@@ -0,0 +1,108 @@
|
||||
/* 全局重置与基础样式 */
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
|
||||
}
|
||||
|
||||
/* 计算器主体 */
|
||||
.calculator {
|
||||
width: 320px;
|
||||
background: #1e1e2f;
|
||||
border-radius: 20px;
|
||||
padding: 20px;
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
|
||||
/* 显示屏 */
|
||||
.display {
|
||||
width: 100%;
|
||||
height: 70px;
|
||||
background: #2a2a3d;
|
||||
border: none;
|
||||
border-radius: 12px;
|
||||
color: #ffffff;
|
||||
font-size: 2rem;
|
||||
text-align: right;
|
||||
padding: 0 20px;
|
||||
margin-bottom: 20px;
|
||||
outline: none;
|
||||
letter-spacing: 1px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
/* 按键网格布局 */
|
||||
.keys {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
/* 通用按键样式 */
|
||||
.key {
|
||||
height: 60px;
|
||||
border: none;
|
||||
border-radius: 12px;
|
||||
font-size: 1.4rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.15s ease;
|
||||
background: #2f2f45;
|
||||
color: #ffffff;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.key:hover {
|
||||
background: #3d3d5c;
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.key:active {
|
||||
transform: translateY(0);
|
||||
box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
|
||||
/* 运算符按钮 */
|
||||
.key.operator {
|
||||
background: #4a4a6a;
|
||||
color: #a5b4fc;
|
||||
}
|
||||
|
||||
.key.operator:hover {
|
||||
background: #5a5a80;
|
||||
}
|
||||
|
||||
/* 清除按钮 */
|
||||
.key.clear {
|
||||
background: #e74c3c;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.key.clear:hover {
|
||||
background: #ff6b5b;
|
||||
}
|
||||
|
||||
/* 等号按钮 */
|
||||
.key.equals {
|
||||
background: linear-gradient(135deg, #667eea, #764ba2);
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.key.equals:hover {
|
||||
background: linear-gradient(135deg, #7b8ff0, #8a5cb8);
|
||||
}
|
||||
|
||||
/* 数字 0 跨两列 */
|
||||
.key.zero {
|
||||
grid-column: span 2;
|
||||
}
|
||||
Reference in New Issue
Block a user