jQuery创建 简单在线计算器

  • 源码大小:41.55KB
  • 所需积分:1积分
  • 源码编号:19JP-3778
  • 浏览次数:742次
  • 最后更新:2023年07月22日
  • 所属栏目:其他
本站默认解压密码:19jp.com 或 19jp_com

简介

本教程将向您展示如何创建一个使用jQuery创建的简单在线计算器。

计算器可以用于加法、减法、乘法和除法等基本运算。

我们将使用HTML和CSS为页面上的元素设置样式,同时利用JavaScript中的事件侦听器根据用户交互修改它们的外观。让我们开始吧! 

如何使用它:

1.为计算器界面创建HTML。复制此代码块并将其添加到您的网页中。

  1. <div class="container">
  2. <div class="row">
  3. <button id="clear" value="">AC</button>
  4. <div class="screen"></div>
  5. </div>
  6. <div class="row">
  7. <button class="digit" value="7">7</button>
  8. <button class="digit" value="8">8</button>
  9. <button class="digit" value="9">9</button>
  10. <button class="operation" id="/">/</button>
  11. </div>
  12. <div class="row">
  13. <button class="digit" value="4">4</button>
  14. <button class="digit" value="5">5</button>
  15. <button class="digit" value="6">6</button>
  16. <button class="operation" id="-">-</button>
  17. </div>
  18. <div class="row">
  19. <button class="digit" value="1">1</button>
  20. <button class="digit" value="2">2</button>
  21. <button class="digit" value="3">3</button>
  22. <button class="operation" id="+">+</button>
  23. </div>
  24. <div class="row">
  25. <button class="digit" value="0">0</button>
  26. <button class="decPoint" value=".">.</button>
  27. <button class="equal" id="eql">=</button>
  28. <button class="operation" id="*">*</button>
  29. </div>
  30. </div>

2.将以下CSS片段添加到您的CSS中。

  1. /* optional */
  2. @font-face {
  3. font-family: 'Digital-7';
  4. src: url('./fonts/Digital-7.ttf') format('embedded-opentype'), /* Internet Explorer */
  5. url('./fonts/Digital-7.ttf') format('woff2'), /* Super Modern Browsers */
  6. url('./fonts/Digital-7.ttf') format('woff'), /* Pretty Modern Browsers */
  7. url('./fonts/Digital-7.ttf') format('truetype'), /* Safari, Android, iOS */
  8. url('./fonts/Digital-7.ttf') format('svg'); /* Legacy iOS */
  9. }
  10.  
  11. * {
  12. box-sizing: border-box;
  13. margin: 0;
  14. padding: 0;
  15. }
  16.  
  17. body {
  18. font-family: 'Digital-7';
  19. font-weight: normal;
  20. font-style: normal;
  21. }
  22.  
  23. /* calculator styles */
  24. .container {
  25. border: 1px dashed black;
  26. min-width: 310px;
  27. max-width: 310px;
  28. margin: 10px auto;
  29. overflow: auto;
  30. padding: 10px;
  31. border-radius: 5px;
  32. }
  33.  
  34. .row {
  35. min-width: 290px;
  36. max-width: 290px;
  37. display: flex;
  38. align-items: center;
  39. justify-content: center;
  40. gap: 10px;
  41. }
  42.  
  43. .screen {
  44. height: 40px;
  45. line-height: 28px;
  46. width: 190px;
  47. text-align: right;
  48. border: 1px solid black;
  49. border-radius: 5px;
  50. font-size: 30px;
  51. padding: 6px;
  52. }
  53.  
  54. button {
  55. background-color: black;
  56. color: white;
  57. font-family: inherit;
  58. border: none;
  59. width: 50px;
  60. padding: 5px 15px;
  61. margin: 5px;
  62. border-radius: 5px;
  63. cursor: pointer;
  64. text-decoration: none;
  65. font-size: 20px;
  66. line-height: 30px;
  67. }
  68.  
  69. button:active {
  70. background-color: lightgrey;
  71. color: black;
  72. }

3.启用计算器应用程序的主要功能。复制JavaScript片段,并将其插入到最新的jQuery JavaScript库之后。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>
  1. var num1 = null;
  2. var num2 = null;
  3. var operator = null;
  4. var total = 0;
  5. var screenDisplay = '';
  6. var numPeriod = 0;
  7.  
  8. $(document).ready(function() {
  9. $('#clear').on('click', function () {
  10. reset()
  11. displayScreen(total);
  12. });
  13.  
  14. $('.digit').on('click', function (e) {
  15. handleDigit(e);
  16. });
  17.  
  18. $('.decPoint').on('click', function (e) {
  19. // Only add the decimal point if there is none present
  20. if (numPeriod == 0) {
  21. handleDigit(e);
  22. numPeriod++;
  23. }
  24. })
  25.  
  26. $('.operation').on('click', function (e) {
  27. if (num1 == null) {
  28. return;
  29. } else if (num2 == null) {
  30. operator = e.target.id;
  31. displayScreen(num1 + operator);
  32. console.log({num1, operator, num2, total})
  33. } else {
  34. /* If both num1 and num2 are full, then push the
  35. existing value to num1 and save the operator */
  36. num1 = compute(num1, num2);
  37. operator = e.target.id;
  38. num2 = null;
  39. displayScreen(num1 + operator);
  40. // console.log({num1, operator, num2, total})
  41. }
  42. });
  43.  
  44. $('.equal').on('click', function (e) {
  45. if (num1) {
  46. if (!operator) {
  47. total = num1;
  48. displayScreen(num1);
  49. // console.log({num1, operator, num2, total})
  50. return;
  51. }
  52. }
  53.  
  54. total = compute(num1, num2);
  55. displayScreen(total);
  56.  
  57. operator = null;
  58. num1 = total;
  59. num2 = null;
  60. });
  61. });
  62.  
  63. function compute(stringA, stringB) {
  64. let a = parseFloat(stringA);
  65. let b = parseFloat(stringB);
  66.  
  67. switch (operator) {
  68. case "/":
  69. return (a / b).toFixed(3);
  70. case "-":
  71. return (a - b).toFixed(3);
  72. case "+":
  73. return (a + b).toFixed(3);
  74. case "*":
  75. return (a * b).toFixed(3);
  76. default:
  77. break;
  78. }
  79. }
  80.  
  81. function displayScreen(text) {
  82. $('.screen').text(text);
  83. screenDisplay = text.toString();
  84. }
  85.  
  86. function handleDigit(e) {
  87. if (num1 == null) {
  88. num1 = e.target.value;
  89. displayScreen(num1);
  90. // console.log({num1, operator, num2, total})
  91. } else if (operator == null) {
  92. num1 += e.target.value;
  93. displayScreen(num1);
  94. // console.log({num1, operator, num2, total})
  95. } else {
  96. if (num2 == null) {
  97. num2 = e.target.value;
  98. displayScreen(num1 + operator + num2);
  99. // console.log({num1, operator, num2, total})
  100. } else {
  101. num2 += e.target.value;
  102. displayScreen(num1 + operator + num2);
  103. // console.log({num1, operator, num2, total})
  104. }
  105. }
  106. }
  107.  
  108. function reset() {
  109. num1 = null;
  110. num2 = null;
  111. operator = null;
  112. total = 0;
  113. numPeriod = 0;
  114. }

预览截图