计算器.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>计算器</title>
  6. <style>
  7. /*网页的格式化*/
  8. *{
  9. margin: 0;
  10. padding: 0;
  11. font-family: "Segoe UI";
  12. box-sizing: border-box;
  13. }
  14. body{
  15. background-color: #f0f0f0;
  16. display: flex;
  17. justify-content: center;
  18. align-items: center;
  19. padding: 20px;
  20. }
  21. .calculator{
  22. background-color: #000;
  23. border-radius: 15px;
  24. width: 100%;
  25. max-width: 400px;
  26. overflow: hidden;
  27. box-shadow: 0 10px 25px rgba(0,0,0,0.3);
  28. }
  29. .display{
  30. background-color: #222222;
  31. color: #fff;
  32. padding: 25px 20px;
  33. text-align: right;
  34. font-size: 30px;
  35. font-weight: 300;
  36. overflow-x: auto;
  37. }
  38. .buttons{
  39. display: grid;
  40. grid-template-columns: repeat(4,1fr);
  41. gap: 1px;
  42. }
  43. button{
  44. background-color: #666666;
  45. color: #fff;
  46. border: none;
  47. padding: 20px;
  48. font-size: 30px;
  49. transition: background-color 0.2s ease;
  50. }
  51. button:hover{
  52. background-color: #888;
  53. }
  54. button:active{
  55. background-color: #aaa;
  56. }
  57. .operator{
  58. background-color: #ff9500;
  59. }
  60. .operator:hover{
  61. background-color: #ffaa33;
  62. }
  63. .operator:active{
  64. background-color: #ffbb33;
  65. }
  66. .clear{
  67. background-color: #ff3b30;
  68. }
  69. .clear:hover{
  70. background-color: #ff4f40;
  71. }
  72. .clear:active{
  73. background-color: #ff8f80;
  74. }
  75. .zero{
  76. grid-column: span 2;
  77. }
  78. </style>
  79. </head>
  80. <body>
  81. <div class="calculator">
  82. <div class="display" id="display">0</div>
  83. <div class="buttons">
  84. <button class="clear" id="clear">C</button>
  85. <button class="operator" data-value="±">±</button>
  86. <button class="operator" data-value="%">%</button>
  87. <button class="operator" data-value="÷">÷</button>
  88. <button data-value="7">7</button>
  89. <button data-value="8">8</button>
  90. <button data-value="9">9</button>
  91. <button class="operator" data-value="×">×</button>
  92. <button data-value="4">4</button>
  93. <button data-value="5">5</button>
  94. <button data-value="6">6</button>
  95. <button class="operator" data-value="-">-</button>
  96. <button data-value="1">1</button>
  97. <button data-value="2">2</button>
  98. <button data-value="3">3</button>
  99. <button class="operator" data-value="+">+</button>
  100. <button class="zero" data-value="0">0</button>
  101. <button data-value=".">.</button>
  102. <button class="operator" data-value="=">=</button>
  103. </div>
  104. </div>
  105. <script>
  106. //获取DOM元素
  107. const display = document.getElementById("display");
  108. const clear = document.getElementById("clear");
  109. //获取所有的指定元素 获取带有data-value属性的所有元素
  110. const buttons=document.querySelectorAll("button[data-value]");
  111. //计算器的状态
  112. let currentValue="0";
  113. let operator=null;
  114. let resetOnNextInput=false;
  115. //更新显示框操作
  116. function updateDisplay(){
  117. display.textContent=currentValue;
  118. }
  119. //处理数字的输入
  120. function handleNumberInput(number){
  121. if (resetOnNextInput){
  122. currentValue=number;
  123. resetOnNextInput=false
  124. }else {
  125. // 0 == '0' true 0 === '0' false
  126. currentValue=currentValue === '0' ?number:currentValue+number;
  127. }
  128. //更新
  129. updateDisplay();
  130. }
  131. //绑定事件监听器
  132. buttons.forEach(
  133. button =>{
  134. button.addEventListener('click',()=>{
  135. const value=button.getAttribute("data-value");
  136. if (!isNaN(value)||value==='0'){
  137. handleNumberInput(value);
  138. }else if(value === '.'){
  139. //执行小数
  140. }else if(value === '='){
  141. //获取结果
  142. }else {
  143. //继续运算
  144. }
  145. });
  146. });
  147. </script>
  148. </body>
  149. </html>