博客列表 >vue实现简易计算器

vue实现简易计算器

leverWang
leverWang原创
2020年09月10日 11:20:273533浏览

html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="vue.js"></script>
  8. <style>
  9. * {
  10. padding: 0;
  11. margin: 0;
  12. }
  13. .item {
  14. cursor: pointer;
  15. }
  16. .item:hover {
  17. background: #ddd
  18. }
  19. .container_box {
  20. display: grid;
  21. grid-template-rows: 60px 60px 60px;
  22. grid-template-columns: 60px 60px 60px;
  23. float: left;
  24. }
  25. .container_box .item {
  26. font-size: 24px;
  27. text-align: center;
  28. border: 1px solid #000;
  29. line-height: 60px;
  30. color: #333;
  31. }
  32. .btns {
  33. display: grid;
  34. grid-template-rows: 60px 60px 60px;
  35. }
  36. .btns .item {
  37. font-size: 24px;
  38. text-align: center;
  39. border: 1px solid #000;
  40. line-height: 60px;
  41. color: #333;
  42. width: 60px
  43. }
  44. .res {
  45. width: 116px;
  46. height: 30px;
  47. border: 1px solid #000;
  48. line-height: 30px;
  49. text-align: center;
  50. cursor: pointer;
  51. cursor: pointer;
  52. display: inline-block;
  53. margin-top: 5px;
  54. }
  55. .res:hover {
  56. background: #ddd
  57. }
  58. .sign {
  59. margin-top: 20px;
  60. font-size: 28px;
  61. font-weight: bold;
  62. padding: 15px;
  63. color: rgb(0, 119, 167);
  64. }
  65. </style>
  66. </head>
  67. <body>
  68. <div class="app">
  69. <div class="container_box" @click="getValue">
  70. <div class="item ">7</div>
  71. <div class="item ">8</div>
  72. <div class="item ">9</div>
  73. <div class="item ">4</div>
  74. <div class="item ">5</div>
  75. <div class="item ">6</div>
  76. <div class="item ">1</div>
  77. <div class="item ">2</div>
  78. <div class="item ">3</div>
  79. <div class="item ">0</div>
  80. <div class="item " style="width:120px">.</div>
  81. </div>
  82. <div class="btns" @click="getSign">
  83. <div class="item ">+</div>
  84. <div class="item ">-</div>
  85. <div class="item ">*</div>
  86. <div class="item ">/</div>
  87. </div>
  88. <div class="res" @click="init">重置</div>
  89. <div class="res" @click="getRes">计算</div>
  90. <div class="sign">
  91. <span>{{num1}}</span>
  92. <span>{{sign}}</span>
  93. <span>{{num2}}</span>
  94. <span>{{equal}}</span>
  95. <span>{{res}}</span>
  96. </div>
  97. </div>
  98. <script src="demo.js"></script>
  99. </body>
  100. </html>

js

  1. var vm = new Vue({
  2. el: ".app",
  3. data: {
  4. num1: "",
  5. num2: "",
  6. sign: "",
  7. res: "",
  8. flag: true,
  9. equal: "",
  10. },
  11. methods: {
  12. //获取数字
  13. getValue(e) {
  14. if (this.flag == true) {
  15. this.num1 += e.target.innerText;
  16. }
  17. if (this.flag == false) {
  18. this.num2 += e.target.innerText;
  19. }
  20. },
  21. //获取运算符
  22. getSign(e) {
  23. this.flag = false;
  24. this.sign = e.target.innerText;
  25. },
  26. //计算
  27. getRes() {
  28. this.equal = "=";
  29. switch (this.sign) {
  30. case "+":
  31. this.res = this.num1 * 1 + this.num2 * 1;
  32. break;
  33. case "-":
  34. this.res = this.num1 - this.num2;
  35. break;
  36. case "*":
  37. this.res = this.num1 * this.num2;
  38. break;
  39. case "/":
  40. this.res = this.num1 / this.num2;
  41. break;
  42. default:
  43. this.res = "未识别的运算符";
  44. break;
  45. }
  46. },
  47. init() {
  48. this.flag = true;
  49. this.num1 = "";
  50. this.num2 = "";
  51. this.sign = "";
  52. this.res = "";
  53. this.equal = "";
  54. },
  55. },
  56. });

效果

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议
P粉0753674952022-12-09 11:04:212楼
你是不是漏了个js没上传?
十二2021-12-15 11:34:211楼
flag的作用是什么?