JavaScript,ES6语法制作简易计算器

超、凢脫俗 2023-01-15 03:44 258阅读 0赞

JavaScript,ES6语法制作计算器

    • 1、先看效果:
    • 2、HTML主体部分
    • 3、css样式部分:
    • 4、js部分

1、先看效果:

在这里插入图片描述
里面所需要的背景图片:在这里插入图片描述

2、HTML主体部分

  1. <div id="counter">
  2. <h2>简易计算器</h2>
  3. <div id="counter_content">
  4. <h3><input id="input1" type="text" value="0" disabled="disabled" ></h3>
  5. <ul>
  6. <li onclick="addStr(7)">7</li>
  7. <li onclick="addStr(8)">8</li>
  8. <li onclick="addStr(9)">9</li>
  9. <li onclick="addStr('+')">+</li>
  10. <li onclick="addStr(4)">4</li>
  11. <li onclick="addStr(5)">5</li>
  12. <li onclick="addStr(6)">6</li>
  13. <li onclick="addStr('-')">-</li>
  14. <li onclick="addStr(1)">1</li>
  15. <li onclick="addStr(2)">2</li>
  16. <li onclick="addStr(3)">3</li>
  17. <li onclick="addStr('*')">×</li>
  18. <li onclick="addStr(0)">0</li>
  19. <li onclick="clears()">C</li>
  20. <li onclick="calc()">=</li>
  21. <li onclick="addStr('/')">÷</li>
  22. </ul>
  23. </div>
  24. <div id="bg"></div>
  25. </div>

3、css样式部分:

  1. * {
  2. padding: 0;
  3. margin: 0;
  4. }
  5. li {
  6. list-style: none;
  7. }
  8. #counter {
  9. width: 500px;
  10. height: 320px;
  11. margin: 50px auto 0;
  12. position: relative;
  13. border: 1px solid green;
  14. }
  15. #counter h2 {
  16. line-height: 30px;
  17. padding-left: 15px;
  18. font-size: 14px;
  19. font-family: arial;
  20. color: #000;
  21. }
  22. #counter a {
  23. font-weight: normal;
  24. text-decoration: none;
  25. color: #fff;
  26. }
  27. #counter a:hover {
  28. text-decoration: underline;
  29. }
  30. #bg {
  31. width: 280px;
  32. height: 200px;
  33. border: 3px solid #680023;
  34. background: #990033;
  35. filter: alpha(opacity=80);
  36. opacity: 0.8;
  37. position: absolute;
  38. left: 50%;
  39. top: 60px;
  40. margin-left: -141px;
  41. }
  42. #counter_content {
  43. width: 250px;
  44. position: absolute;
  45. top: 80px;
  46. left: 130px;
  47. z-index: 1;
  48. }
  49. #counter_content h3 {
  50. margin-bottom: 10px;
  51. }
  52. #counter_content h3 input {
  53. border: none;
  54. width: 223px;
  55. height: 30px;
  56. line-height: 30px;
  57. padding: 0 10px;
  58. background: url(../images/ico.png) no-repeat;
  59. text-align: right;
  60. color: #333;
  61. font-size: 14px;
  62. font-weight: bold;
  63. }
  64. #counter_content ul {
  65. width: 250px;
  66. }
  67. #counter_content li {
  68. width: 60px;
  69. height: 30px;
  70. line-height: 30px;
  71. float: left;
  72. background: url(../images/ico.png) no-repeat -303px 0;
  73. text-align: center;
  74. color: #fff;
  75. cursor: pointer;
  76. margin: 0 1px 4px 0;
  77. }
  78. #counter_content .active {
  79. background: url(../images/ico.png) no-repeat -243px 0;
  80. }
  81. #counter p {
  82. width: 500px;
  83. position: absolute;
  84. bottom: 20px;
  85. left: 0;
  86. color: #ff3333;
  87. text-align: center;
  88. font-size: 12px;
  89. }

4、js部分

  1. <script>
  2. function addStr(s){
  3. //获取input 给他添加字符
  4. let inp = document.getElementById("input1");
  5. inp.value = inp.value + s;
  6. //inp 现在的值等于原来的值+s
  7. //去除首个零
  8. let inpv = inp.value;
  9. let arr = inpv.split("");
  10. if(arr[0] == "0"){
  11. arr.shift();
  12. inpv = arr.join("");
  13. inp.value = inpv;
  14. }
  15. }
  16. //进行计算
  17. function calc(){
  18. let inp = document.getElementById("input1");
  19. //这里用到了eval函数
  20. let val = eval(inp.value);
  21. //把文本框里面的值当js计算一下赋值给val
  22. inp.value = val;
  23. }
  24. //归零键
  25. function clears(){
  26. let inp = document.getElementById("input1");
  27. inp.value = "0";
  28. }
  29. </script>

发表评论

表情:
评论列表 (有 0 条评论,258人围观)

还没有评论,来说两句吧...

相关阅读