您现在的位置是:网站首页> 编程资料编程资料
微信小程序实现简易计算器功能_javascript技巧_
2023-05-24
381人已围观
简介 微信小程序实现简易计算器功能_javascript技巧_
本文实例为大家分享了微信小程序实现简易计算器的具体代码,供大家参考,具体内容如下

实现代码:
{{num}} {{op}} C DEL % ÷ 7 8 9 × 4 5 6 - 1 2 3 + 0 . =
/* pages/computer.wxss */ page{ display: flex; flex-direction: column; height: 100% } .result{ flex: 1; background-color: #f3f6fe; position: relative; } .result_num{ position: absolute; font-size: 27pt; bottom: 5vh; right: 3vw; } .result_op{ font-size: 15pt; position: absolute; bottom: 1vh; right: 3vw; } .btns{ flex: 1; display: flex; flex-direction: column; font-size: 17pt; border-top: 1px solid #ccc; border-left: 1px solid #ccc; } .btns>view{ flex: 1; display: flex; } .btns>view>view{ flex:1; border-right: 1px solid #ccc; border-bottom: 1px solid #ccc; box-sizing: border-box; display: flex; align-items: center; justify-content: center; } .btns>view:last-child>view:first-child{ flex: 2; } .btns>view:first-child>view:first-child{ color: #f00; } .btns>view>view:last-child{ color: #fc8e00; } .bg{ background-color: #eee; }// pages/computer.js Page({ /** * 页面的初始数据 */ data: { num:'0', op:'' }, result:null, isClear:false, numBtn:function(e){ var num = e.target.dataset.val if(this.data.num==='0'||this.isClear){ this.setData({num:num}) this.isClear=false }else{ this.setData({num:this.data.num+num}) } }, opBtn:function(e){ var op = this.data.op var num = Number(this.data.num) var val = e.target.dataset.val if(val==='/'){ this.setData({op:'÷'}) }else if(val==='*'){ this.setData({op:'×'}) }else{ this.setData({op:val}) } //用于多次按计算按钮时,避免重复计算的问题 if(this.isClear){ return } this.isClear=true if(this.result===null||this.result===undefined){ this.result = num return } if(op==='+'){ this.result=this.result+num }else if(op==='-'){ this.result=this.result-num }else if(op==='×'){ this.result=(this.result*num).toFixed(2) }else if(op==='÷'){ this.result=this.result/num }else if(op==='%'){ this.result=this.result%num } this.setData({num:this.result}) }, dotBtn:function(){ if(this.isClear){ this.setData({num:'0.'}) this.isClear=false return } if(this.data.num.indexOf('.')>=0){ return } this.setData({num:this.data.num+'.'}) }, delBtn:function(){ var temp = this.data.num.toString() var num = temp.substr(0,this.data.num.length-1); this.setData({num:num===''?'0':num}) }, resetBtn:function(){ this.result=null this.isClear=false this.setData({num:'0',op:''}) } })computer.json
{ "navigationBarTitleText":"计算器", "navigationBarBackgroundColor":"#fff", "navigationBarTextStyle":"black", "usingComponents": {} }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
您可能感兴趣的文章:
相关内容
- vue获取token如何实现token登录_vue.js_
- vue组件传值(高级)、属性传值、反向传值、跨级传值实例详解_vue.js_
- 关于axios配置多个请求地址(打包后可通过配置文件修改)_vue.js_
- Vue自定义指令的使用详细介绍_vue.js_
- react父组件更改props子组件无法刷新原因及解决方法_React_
- 多个vue项目实现共用一个node-modules文件夹_vue.js_
- 关于vue-i18n在单文件js中的使用_vue.js_
- vue中iframe使用以及结合postMessage实现跨域通信_vue.js_
- Vue混入与插件的使用介绍_vue.js_
- JS网页repaint与reflow 的区别及优化方式_JavaScript_
