经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序计算器实例详解
来源:jb51  时间:2021/7/19 11:49:53  对本文有异议

微信小程序计算器实例,供大家参考,具体内容如下

index.wxml

  1. <view class="content">
  2. <view class="num">{{num}}</view>
  3. <view class="operotor">{{op}}</view>
  4. </view>
  5. <view class="entry">
  6. <view>
  7. <view class="item" bindtap="resetBtn">c</view>
  8. <view class="item" bindtap="delBtn">DEL</view>
  9. <view class="item" bindtap="opBtn" data-val="%">%</view>
  10. <view class="item" bindtap="opBtn" data-val="/">÷</view>
  11. </view>
  12. <view>
  13. <view class="item" bindtap="numBtn" data-val="7">7</view>
  14. <view class="item" bindtap="numBtn" data-val="8">8</view>
  15. <view class="item" bindtap="numBtn" data-val="9">9</view>
  16. <view class="item" bindtap="opBtn" data-val="*">x</view>
  17. </view>
  18. <view>
  19. <view class="item" bindtap="numBtn" data-val="4">4</view>
  20. <view class="item" bindtap="numBtn" data-val="5">5</view>
  21. <view class="item" bindtap="numBtn" data-val="6">6</view>
  22. <view class="item" bindtap="opBtn" data-val="-">-</view>
  23. </view>
  24. <view>
  25. <view class="item" bindtap="numBtn" data-val="1">1</view>
  26. <view class="item" bindtap="numBtn" data-val="2">2</view>
  27. <view class="item" bindtap="numBtn" data-val="3">3</view>
  28. <view class="item" bindtap="opBtn" data-val="+">+</view>
  29. </view>
  30. <view>
  31. <view class="item tow" bindtap="numBtn" data-val="0">0</view>
  32. <view class="item one" bindtap="dotBtn" data-val=".">.</view>
  33. <view class="item one" bindtap="opBtn" data-val="=">=</view>
  34. </view>
  35. </view>

index.css

  1. page {
  2. display: flex;
  3. flex-direction: column;
  4. height: 100%;
  5. }
  6. .content {
  7. flex: 1;
  8. background-color: #f3f6fe;
  9. position: relative;
  10. }
  11. .content .num {
  12. position: absolute;
  13. font-size: 27pt;
  14. bottom: 5vh;
  15. right: 3vw;
  16. }
  17. .content .operotor {
  18. font-size: 15pt;
  19. position: absolute;
  20. bottom: 1vh;
  21. right: 3vw;
  22. }
  23. .entry {
  24. flex: 1;
  25. font-size: 17pt;
  26. border-top: 1rpx solid #ccc;
  27. }
  28. .entry .item {
  29. flex: 1;
  30. padding: 30rpx 0;
  31. text-align: center;
  32. flex-basis: 25%;
  33. border-left: 1rpx solid #ccc;
  34. border-bottom: 1rpx solid #ccc;
  35. }
  36. .entry > view {
  37. display: flex;
  38. }
  39. .entry > view .tow {
  40. flex: 2;
  41. }
  42. .entry > view .one {
  43. flex: 1;
  44. }

index.js

  1. Page({
  2. data: {
  3. num: "", // 存储数字
  4. op: "" //存储运算符
  5. },
  6. result: null,
  7. isClear: false,
  8.  
  9. numBtn: function(e) {
  10. var num = e.target.dataset.val
  11. //console.log(num) 得到data-val的值
  12. console.log(this.isClear)
  13. if (this.data.num === "0" || this.isClear) {
  14. this.setData({ num: num })
  15. this.isClear = false
  16. } else {
  17. this.setData({ num: this.data.num + num })
  18. }
  19. },
  20.  
  21. opBtn: function(e) {
  22. var op = this.data.op
  23. var num = Number(this.data.num)
  24. this.setData({ op: e.target.dataset.val })
  25. if (this.isClear) {
  26. return
  27. }
  28. this.isClear = true
  29. if (this.result === null) {
  30. this.result = num
  31. return
  32. }
  33. if (op === "+") {
  34. this.result = this.result + num
  35. } else if (op === "-") {
  36. this.result = this.result - num
  37. } else if (op === "*") {
  38. this.result = this.result * num
  39. } else if (op === "/") {
  40. this.result = this.result / num
  41. } else if (op === "%") {
  42. this.result = this.result % num
  43. }
  44. this.setData({ num: this.result })
  45. },
  46.  
  47. dotBtn: function() {
  48. if (this.isClear) {
  49. this.setData({ num: "0." })
  50. this.isClear = false
  51. return
  52. }
  53. if (this.data.num.indexOf(".") >= 0) {
  54. return
  55. }
  56. this.setData({ num: this.data.num + "." })
  57. },
  58. delBtn: function() {
  59.  
  60. var num = this.data.num.substr(0, this.data.num.length - 1)
  61. this.setData({ num: num === "" ? "0" : num })
  62. },
  63. resetBtn: function() {
  64. this.result = null
  65. this.isClear = false
  66. this.setData({ num: "0", op: "" })
  67. }
  68. })

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号