当前位置:   article > 正文

小程序第三章作业

小程序第三章作业

1.使用代码制作计算器

第一步 打开我们的微信开发工具,创建

第二步 创建文件夹zy1,目录.js、.json、.wxml、.wxss。

第三步 开始写简易计算器的界面,打开zy1目录下的zy1.wxml,在这里通过标签设置我们的界面,代码如下:

  1. //zy1.wxml
  2. <view class="screen">
  3. <view>{{result}}</view>
  4. </view>
  5. <view class="bottom">
  6. <view class="btngroup">
  7. <button id="{{id1}}" bindtap="clickButton" hover-class="shadow" class="btn-item gry">C</button>
  8. <button id="{{id2}}" bindtap="clickButton" hover-class="shadow" class="btn-item gry"></button>
  9. <button id="{{id3}}" bindtap="clickButton" hover-class="shadow" class="btn-item gry">#</button>
  10. <button id="{{id4}}" bindtap="clickButton" hover-class="shadow" class="btn-item orange">/</button>
  11. </view>
  12. <view class="btngroup">
  13. <button id="{{id5}}" bindtap="clickButton" hover-class="shadow" class="btn-item white">7</button>
  14. <button id="{{id6}}" bindtap="clickButton" hover-class="shadow" class="btn-item white">8</button>
  15. <button id="{{id7}}" bindtap="clickButton" hover-class="shadow" class="btn-item white">9</button>
  16. <button id="{{id8}}" bindtap="clickButton" hover-class="shadow" class="btn-item orange">*</button>
  17. </view>
  18. <view class="btngroup">
  19. <button id="{{id9}}" bindtap="clickButton" hover-class="shadow" class="btn-item white">4</button>
  20. <button id="{{id10}}" bindtap="clickButton" hover-class="shadow" class="btn-item white">5</button>
  21. <button id="{{id11}}" bindtap="clickButton" hover-class="shadow" class="btn-item white">6</button>
  22. <button id="{{id12}}" bindtap="clickButton" hover-class="shadow" class="btn-item orange">-</button>
  23. </view>
  24. <view class="btngroup">
  25. <button id="{{id13}}" bindtap="clickButton" hover-class="shadow" class="btn-item white">1</button>
  26. <button id="{{id14}}" bindtap="clickButton" hover-class="shadow" class="btn-item white">2</button>
  27. <button id="{{id15}}" bindtap="clickButton" hover-class="shadow" class="btn-item white">3</button>
  28. <button id="{{id16}}" bindtap="clickButton" hover-class="shadow" class="btn-item orange">+</button>
  29. </view>
  30. <view class="btngroup">
  31. <button id="{{id17}}" bindtap="clickButton" hover-class="shadow" class="btn-item zero white">0</button>
  32. <button id="{{id18}}" bindtap="clickButton" hover-class="shadow" class="btn-item white">.</button>
  33. <button id="{{id19}}" bindtap="equals" hover-class="shadow" class="btn-item orange">=</button>
  34. </view>
  35. </view>

第四步 打开zy1目录下的zy1.wxss输入下面代码块中的命令对整个界面的布局进行调整

  1. //zy1.wxss
  2. page{
  3. background: #000;
  4. }
  5. .btngroup{
  6. display: flex;
  7. flex-direction: row;
  8. }
  9. .btn-item{
  10. width: 189rpx;
  11. text-align: center;
  12. line-height: 145rpx;
  13. border-radius: 0;
  14. }
  15. .zero{
  16. width: 376rpx;
  17. }
  18. .bottom{
  19. position: fixed;
  20. bottom: 0;
  21. }
  22. .white{
  23. background:#EFEFED;
  24. }
  25. .orange{
  26. background: #FC9504;
  27. }
  28. .gry{
  29. background: #CFCFCF;
  30. }
  31. .screen{
  32. position: fixed;
  33. bottom: 750rpx;
  34. word-wrap: break-word;
  35. width: 745rpx;
  36. }
  37. .screen view{
  38. color: #fff;
  39. font-size: 30px;
  40. text-align: right;
  41. margin-right: 30rpx;
  42. }
  43. icon{
  44. position: absolute;
  45. left: 55rpx;
  46. top:37rpx;
  47. }
  48. button::after{
  49. border-radius: 0;
  50. }
  51. .shadow{
  52. background: #999;
  53. }

第五步 开始编写zy1目录下zy1.js代码

  1. Page({
  2. data: {
  3. id1:"clear",
  4. id2:"back",
  5. id3:"history",
  6. id4:"div",
  7. id5:"num_7",
  8. id6:"num_8",
  9. id7:"num_9",
  10. id8:"mul",
  11. id9:"num_4",
  12. id10:"num_5",
  13. id11:"num_6",
  14. id12:"sub",
  15. id13:"num_1",
  16. id14:"num_2",
  17. id15:"num_3",
  18. id16:"add",
  19. id17:"num_0",
  20. id18:"dot",
  21. id19:"equals",
  22. result:"0",
  23. dotSign:false,
  24. },
  25. clickButton: function(e){
  26. console.log(e);
  27. var btnValue=e.target.id;
  28. var res=this.data.result;
  29. var newDotSign=this.data.dotSign;
  30. if(btnValue>="num_0"&&btnValue<="num_9"){
  31. console.log(btnValue.split('_'));
  32. var num=btnValue.split('_')[1];
  33. if (res == "0" || res.charAt(res.length - 1) == '∞'){
  34. res=num;
  35. }else{
  36. res=res+num;
  37. }
  38. }else{
  39. if (btnValue=="dot"){
  40. if(!newDotSign){
  41. res=res+'.';
  42. newDotSign=true;
  43. }
  44. }else if(btnValue=="clear"){
  45. res="0";
  46. newDotSign = false;
  47. }else if(btnValue=="back"){
  48. var length=res.length;
  49. if(length>1){
  50. res=res.substr(0,length-1);
  51. }else{
  52. res="0";
  53. }
  54. }else if(btnValue=="add"||btnValue=="sub"||btnValue=="mul"||btnValue=="div"){
  55. newDotSign=false;
  56. var sign;
  57. switch (btnValue){
  58. case "add":sign = "+"; break;
  59. case "sub":sign = "-"; break;
  60. case "mul":sign = "*"; break;
  61. case "div":sign = "/"; break;
  62. }
  63. if(!isNaN(res.charAt(res.length-1))){
  64. res = res + sign;
  65. }
  66. }
  67. }
  68. this.setData({
  69. result: res,
  70. dotSign:newDotSign,
  71. });
  72. },
  73. equals:function(){
  74. var str=this.data.result;
  75. var strArr=[];
  76. var item='';
  77. var temp=0;
  78. for(var i=0;i<=str.length;i++){
  79. var ch=str.charAt(i);
  80. if((ch!=''&&ch>=0&&ch<=9)||ch=="."){
  81. item=item+ch;
  82. }else{
  83. strArr[temp]=item;
  84. temp++;
  85. strArr[temp]=ch;
  86. temp++;
  87. item='';
  88. }
  89. }
  90. if (isNaN(strArr[strArr.length-1])){
  91. strArr.pop();
  92. }
  93. var res = parseInt(strArr[0]);
  94. var num;
  95. for(var i=1;i<strArr.length;i=i+2){
  96. if(res=="∞"){
  97. break;
  98. }
  99. num=strArr[i+1]*1;
  100. switch (strArr[i]){
  101. case '+': res = res + num; break;
  102. case '-': res = res - num; break;
  103. case '*': res = res * num; break;
  104. case '/':
  105. if(num!=0){
  106. res = res / num;
  107. }else{
  108. res="∞";
  109. }
  110. break;
  111. }
  112. }
  113. this.setData({
  114. result:"="+res,
  115. });
  116. }
  117. });

第六步  补充zy1目录下的zy1.json文件

  1. {
  2. "navigationBarBackgroundColor": "#fff",
  3. "navigationBarTitleText": "计算器",
  4. "navigationBarTextStyle": "black",
  5. "usingComponents": {}
  6. }

运行结果:

2.页面布局效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/252594
推荐阅读
相关标签
  

闽ICP备14008679号