微信小程序实现简单计算器功能
程序员文章站
2022-03-10 20:57:56
微信小程序:简单计算器,供大家参考,具体内容如下对于才接触小程序不久的人来说,想要直接上手一个实用性强的项目难度很大,想要快速熟悉小程序的使用,我们可以先尝试着做一个简单的计算器。运行截图计算器对于界...
微信小程序:简单计算器,供大家参考,具体内容如下
对于才接触小程序不久的人来说,想要直接上手一个实用性强的项目难度很大,想要快速熟悉小程序的使用,我们可以先尝试着做一个简单的计算器。
运行截图
计算器对于界面美观的要求并不高,只是一些view以及button控件的组合,所以并不需要在界面上费很多功夫。重要的是逻辑层,之所以选择计算器作为上手的第一个项目,因为计算器的逻辑可简可繁,完全可以适应新手对小程序的掌握程度。
主要代码
js:
page({ data: { result:"0", id1:"clear", id2:"back", id3:"time", id4:"div", id5:"mul", id6:"sub", id7:"add", id8:"dot", id9:"eql", id10:"num_0", id11:"num_1", id12:"num_2", id13:"num_3", id14:"num_4", id15:"num_5", id16:"num_6", id17:"num_7", id18:"num_8", id19:"num_9", buttondot:false, is_time:false }, clickbutton: function (e) { console.log(e); var buttonval = e.target.id; var res = this.data.result; if(this.data.is_time==true){ res=0 } var newbuttondot=this.data.buttondot; var sign; if (buttonval >= "num_0" && buttonval <= "num_9") { var num=buttonval.split('_')[1]; if (res == "0" || ((res.length-0) -(length-1)) == "=") { res = num; } else { res = res + num; } } else{ if(buttonval=="dot") { if(!newbuttondot) { res = res + '.'; } } else if(buttonval=="clear") { res='0'; } else if(buttonval=="back") { var length=res.length; if(length>1) { res=res.substr(0,length-1); } else{ res='0'; } } else if (buttonval == "div" || buttonval == "mul" || buttonval == "sub" || buttonval == "add") { if(res.length){} else{ res=json.stringify(res) } var is_sign=res.substr(res.length-1,res.length) if(is_sign=="+"||is_sign=="-"||is_sign=="×"||is_sign=="÷"){ res=res.substr(0,res.length-1); } switch(buttonval){ case "div": sign ='÷'; break; case "mul": sign ='×'; break; case "sub": sign='-'; break; case "add": sign='+'; break; } if(!isnan(res.length)) { res.length-1; res=res+sign; } } } this.setdata({ is_time:false, result: res, buttondot:newbuttondot, }); }, equal: function(e){ var str=this.data.result; var item= ''; var strarray = []; var temp=0; for(var i=0;i<=str.length;i++){ var s=str.charat(i); if((s!='' && s>='0' && s<='9') || s=='.'){ item=item+s; } else{ strarray[temp]=item; temp++; strarray[temp]=s; temp++; item=''; } } if(isnan(strarray[strarray.length-1])) { strarray.pop(); } var num; var res=strarray[0]*1; for(var i=1;i<=strarray.length;i=i+2){ num=strarray[i+1]; switch(strarray[i]){ case "-": res = (res-0)- (num-0); break; case "+": res = (res-0) + (num-0); break; case "×": res = (res-0)* (num-0); break; case "÷": if(num!='0') { res = (res-0)/ (num-0); } else { res ='∞'; break; } break; } } this.setdata({ result:res, }); }, time:function(e){ var util=require("../../utils/util.js"); var time=util.formattime(new date()); this.setdata({ result:time, is_time:true }); }})
wxml
<!--index.wxml--> <view class="project_name">简单计算器</view> <view class="screen_content"> <view class="screen">{{result}}</view> </view> <view class="content"> <view class="buttongroup"> <button id="{{id1}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">c</button> <button id="{{id2}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">bs</button> <button id="{{id3}}" bindtap="time" class="buttonitem color" hover-class="shadow"> <icon type="waiting" color="#66cc33"></icon> </button> <button id="{{id4}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">÷</button> </view> <view class="buttongroup"> <button id="{{id17}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">7</button> <button id="{{id18}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">8</button> <button id="{{id19}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">9</button> <button id="{{id5}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">×</button> </view> <view class="buttongroup"> <button id="{{id14}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">4</button> <button id="{{id15}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">5</button> <button id="{{id16}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">6</button> <button id="{{id6}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">-</button> </view> <view class="buttongroup"> <button id="{{id11}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">1</button> <button id="{{id12}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">2</button> <button id="{{id13}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">3</button> <button id="{{id7}}" bindtap="clickbutton" class="buttonitem color" hover-class="shadow">+</button> </view> <view class="buttongroup"> <button id="{{id10}}" bindtap="clickbutton" class="buttonitem1 color" hover-class="shadow">0</button> <button id="{{id8}}" bindtap="clickbutton" class="buttonitem1 color" hover-class="shadow">.</button> <button id="{{id9}}" bindtap="equal" class="equal" hover-class="shadow">=</button> </view> </view>
wxss:
/**index.wxss**/ page{ background: #f5f5f5; } .project_name{ position:absolute; top:25px; width:100%; text-align: center; font-size: 30px; } .screen_content{ position: fixed; color: #1b1717; background: #fff; font-size: 40px; bottom: 390px; text-align: right; height:100px; width: 100%; word-wrap: break-word; border-top:1px solid #a8a8a8; border-bottom:1px solid #a8a8a8; } .screen{ position: absolute; font-size: 40px; text-align: right; bottom:0px; width: 96%; left:2%; word-wrap: break-word; } .content{ position: fixed; bottom: 0; } .buttongroup{ display: flex; flex-direction: row; } .buttonitem{ text-align: center; line-height: 120rpx; width: 25%; border-radius: 0; } .buttonitem1{ width: 192rpx; text-align: center; line-height: 120rpx; border-radius: 0; } icon{ position: absolute; top: 20%; left: 67rpx; } .color{ background: #fff; } .equal{ width: 380rpx; text-align: center; line-height: 120rpx; border-radius: 0; background: #fff; } .shadow{ background: #e9ebe9; }
后记
我这里只是对加减程进行了粗略的处理,但是这样的一个计算器也已经具备了初步的功能,随着我们掌握程度的加深,我们还可以添加平方,开方等更复杂的功能,从而熟练掌握小程序的开发。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。