原文链接:https://mp.weixin.qq.com/s/gYF7GjTRpeZNoKPAPI9aXA

1

概述

前几日QQ群里的朋友问我有没有计算器小程序案例,今天我们说下小程序计算器,然后就分享这样的小案例。希望对大家有所帮助

不多说了,二当家要上图来啦!

快去拿个小板凳,坐等更多更新

注意:如若需要请联系微信geekxz

2

wxml

<view class="content">
 <view class="layout-top">
   <view class="screen">
    {{screenData}}
   </view>
 </view>
 <view class="layout-bottom">
   <view class="btnGroup">
     <view class="item orange" bindtap="clickBtn" id="{{idc}}">С</view>
     <view class="item orange" bindtap="clickBtn" id="{{idb}}">←</view>
     <!--<view class="item orange" bindtap="clickBtn" id="{{idt}}">+/-</view>-->
     <view class="item orange iconBtn" bindtap="history">
         <icon type="{{iconType}}" color="{{iconColor}}" class="icon" size="25"/>
     </view>
     <view class="item orange" bindtap="clickBtn" id="{{idadd}}">+</view>
   </view>
   <view class="btnGroup">
     <view class="item blue" bindtap="clickBtn" id="{{id9}}">9</view>
     <view class="item blue" bindtap="clickBtn" id="{{id8}}">8</view>
     <view class="item blue" bindtap="clickBtn" id="{{id7}}">7</view>
     <view class="item orange" bindtap="clickBtn" id="{{idj}}">-</view>
   </view>
   <view class="btnGroup">
     <view class="item blue" bindtap="clickBtn" id="{{id6}}">6</view>
     <view class="item blue" bindtap="clickBtn" id="{{id5}}">5</view>
     <view class="item blue" bindtap="clickBtn" id="{{id4}}">4</view>
     <view class="item orange" bindtap="clickBtn" id="{{idx}}">×</view>
   </view>
   <view class="btnGroup">
     <view class="item blue" bindtap="clickBtn" id="{{id3}}">3</view>
     <view class="item blue" bindtap="clickBtn" id="{{id2}}">2</view>
     <view class="item blue" bindtap="clickBtn" id="{{id1}}">1</view>
     <view class="item orange" bindtap="clickBtn" id="{{iddiv}}">÷</view>
   </view>
   <view class="btnGroup">
     <view class="item blue zero" bindtap="clickBtn" id="{{id0}}">0</view>
     <view class="item blue" bindtap="clickBtn" id="{{idd}}">.</view>
     <view class="item orange" bindtap="clickBtn" id="{{ide}}">=</view>
   </view>
 </view>
</view>

3

js

Page({
 data:{
   idb:"back",
   idc:"clear",
   idt:"toggle",
   idadd:"+",
   id9:"9",
   id8:"8",
   id7:"7",
   idj:"-",
   id6:"6",
   id5:"5",
   id4:"4",
   idx:"×",
   id3:"3",
   id2:"2",
   id1:"1",
   iddiv:"÷",
   id0:"0",
   idd:".",
   ide:"=",
   screenData:"0",
   operaSymbo:{"+":"+","-":"-","×":"*","÷":"/",".":"."},
   lastIsOperaSymbo:false,
   iconType:'waiting_circle',
   iconColor:'white',
   arr:[],
   logs:[]
 },
 onLoad:function(options){
   // 页面初始化 options为页面跳转所带来的参数
 },
 onReady:function(){
   // 页面渲染完成
 },
 onShow:function(){
   // 页面显示
 },
 onHide:function(){
   // 页面隐藏
 },
 onUnload:function(){
   // 页面关闭
 },
 clickBtn:function(event){
   var id = event.target.id;
   if(id == this.data.idb){  //退格←
     var data = this.data.screenData;
     if(data == "0"){
         return;
     }
     data = data.substring(0,data.length-1);
     if(data == "" || data == "-"){
         data = 0;
     }
     this.setData({"screenData":data});
     this.data.arr.pop();
   }else if(id == this.data.idc){  //清屏C
     this.setData({"screenData":"0"});
     this.data.arr.length = 0;
   }else if(id == this.data.idt){  //正负号+/-
     var data = this.data.screenData;
     if(data == "0"){
         return;
     }
     var firstWord = data.charAt(0);
     if(data == "-"){
       data = data.substr(1);
       this.data.arr.shift();
     }else{
       data = "-" + data;
       this.data.arr.unshift("-");
     }
     this.setData({"screenData":data});
   }else if(id == this.data.ide){  //等于=
     var data = this.data.screenData;
     if(data == "0"){
         return;
     }
     //eval是js中window的一个方法,而微信页面的脚本逻辑在是在JsCore中运行,JsCore是一个没有窗口对象的环境,所以不能再脚本中使用window,也无法在脚本中操作组件                
     //var result = eval(newData);          
     var lastWord = data.charAt(data.length);
     if(isNaN(lastWord)){
       return;
     }
     var num = "";
     var lastOperator = "";
     var arr = this.data.arr;
     var optarr = [];
     for(var i in arr){
       if(isNaN(arr[i]) == false || arr[i] == this.data.idd || arr[i] == this.data.idt){
         num += arr[i];
       }else{
         lastOperator = arr[i];
         optarr.push(num);
         optarr.push(arr[i]);
         num = "";
       }
     }
     optarr.push(Number(num));
     var result = Number(optarr[0])*1.0;
     console.log(result);
     for(var i=1; i<optarr.length; i++){
       if(isNaN(optarr[i])){
           if(optarr[1] == this.data.idadd){
               result += Number(optarr[i + 1]);
           }else if(optarr[1] == this.data.idj){
               result -= Number(optarr[i + 1]);
           }else if(optarr[1] == this.data.idx){
               result *= Number(optarr[i + 1]);
           }else if(optarr[1] == this.data.iddiv){
               result /= Number(optarr[i + 1]);
           }
       }
     }
     //存储历史记录
     this.data.logs.push(data +'='+ result);
     wx.setStorageSync("calclogs",this.data.logs);
     this.data.arr.length = 0;
     this.data.arr.push(result);
     this.setData({"screenData":result+""});
   }else{
     if(this.data.operaSymbo[id]){ //如果是符号+-*/
       if(this.data.lastIsOperaSymbo || this.data.screenData == "0"){
         return;
       }
     }
     var sd = this.data.screenData;
     var data;
     if(sd == 0){
       data = id;
     }else{
       data = sd + id;
     }
     this.setData({"screenData":data});
     this.data.arr.push(id);
     if(this.data.operaSymbo[id]){
       this.setData({"lastIsOperaSymbo":true});
     }else{
       this.setData({"lastIsOperaSymbo":false});
     }
   }
 },
 history:function(){
   wx.navigateTo({
     url:'../history/history'
   })
 }
})

4   css

.content {
   height: 100%;
   display: flex;
   flex-direction: column;
   align-items: center;
   background-color: #ccc;
   font-family: "Microsoft YaHei";
   overflow-x: hidden;
}
.layout-top{
   width: 100%;
   margin-bottom: 30rpx;
}
.layout-bottom{
   width: 100%;
}
.screen {
   text-align: right;
   width: 100%;
   line-height: 260rpx;
   padding: 0 10rpx;
   font-weight: bold;
   font-size: 60px;
   box-sizing: border-box;
   border-top: 1px solid #fff;
}
.btnGroup {
   display: flex;
   flex-direction: row;
   flex: 1;
   width: 100%;
   height: 5rem;
   background-color: #fff;
}
.item {
   width:25%;
   display: flex;
   align-items: center;
   flex-direction: column;
   justify-content: center;
   margin-top: 1px;
   margin-right: 1px;
}
.item:active {
   background-color: #ff0000;
}
.zero{
   width: 50%;
}
.orange {
   color: #fef4e9;
   background: #f78d1d;
   font-weight: bold;
}
.blue {
   color:#d9eef7;
   background-color: #0095cd;
}
.iconBtn{
   display: flex;
}
.icon{
  display: flex;
  align-items: center;
  width:100%;
  justify-content: center;
}

以上代码为效果图

文末福利:

福利一:前端,Java,产品经理,微信小程序,Python等100G资源合集大放送:https://www.jianshu.com/p/e8197d4d9880

福利二:微信小程序入门与实战全套详细视频教程。

【领取方法】

关注 【编程微刊】微信公众号:

回复【小程序demo】一键领取130个微信小程序源码demo资源。

回复【领取资源】一键领取前端,Java,产品经理,微信小程序,Python等资源合集100G资源大放送。

强力推荐微信小程序之简易计算器,很适合小白程序员的更多相关文章

  1. 微信小程序开发简易计算器改进版

    微信小程序开发计算器有多种方法,但是大部分代码比较复杂.不容易理解.本案例进行了改进,主要是组件bindtap属性绑定的自定义函数clickBtn(),采用了switch语句,使得代码结构更加清晰,学 ...

  2. 微信小程序< 3 > ~ 微信小程序开源项目合集

    简介 移动开发者想学习微信小程序需要学习一点HTML ,CSS和JS才能够比较快速的上手,参考自己学习Android学习过程,阅读源码是一个很好的方式,所以才收集了一些WeApp的开源项目. awes ...

  3. 微信小程序开源项目库汇总

    最近做了一个微信小程序开源项目库汇总,里面集合了OpenDigg 上的优质的微信小程序开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个star. UI组件 ...

  4. 微信小程序开源项目库集合

    UI组件 weui-wxss ★852 - 同微信原生视觉体验一致的基础样式库 Wa-UI ★122 - 针对微信小程序整合的一套UI库 wx-charts ★105 - 微信小程序图表工具 wema ...

  5. 微信小程序一:微信小程序UI组件、开发框架、实用库

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/8079095.html 内容持续更新,维护中 邮箱 ...

  6. 微信小程序基础之开源项目库汇总

    awesome-github-wechat-weapp 是由OpenDigg整理并维护的微信小程序开源项目库集合.我们会定期同步OpenDigg上的项目到这里,也欢迎各位提交项目给我们. (链接:ht ...

  7. 微信小程序UI组件、开发框架、实用库...

    UI组件 weui-wxss ★852 - 同微信原生视觉体验一致的基础样式库 Wa-UI ★122 - 针对微信小程序整合的一套UI库 wx-charts ★105 - 微信小程序图表工具 wema ...

  8. 微信小程序框架集合

    UI组件 weui-wxss ★852 - 同微信原生视觉体验一致的基础样式库 Wa-UI ★122 - 针对微信小程序整合的一套UI库 wx-charts ★105 - 微信小程序图表工具 wema ...

  9. 微信小程序开源

    | UI组件 | | | | | | | | | weui-wxss ★1873 - 同微信原生视觉体验一致的基础样式库 | | | | | | zanui-weapp ★794 - 好用易扩展的小程 ...

随机推荐

  1. 03007_JDBC概述

    1.JDBC概述 (1)JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用J ...

  2. 开源 java CMS - FreeCMS2.3会员积分记录

    原文地址:http://javaz.cn/site/javaz/site_study/info/2015/28995.html​ 项目地址:http://www.freeteam.cn/ 积分记录 从 ...

  3. 相似group by的分组计数功能

    之前同事发过一个语句,实现的功能比較简单,相似group by的分组计数功能,由于where条件有like,又无法用group by来实现. SELECT a.N0,b.N1,c.N2,d.N3,e. ...

  4. Day2下午解题报告

    预计分数:100+100+30=230 实际分数:100+100+30=230人品爆发&&智商爆发&&手感爆发 T3数据好水,,要是把数组开大一点的话还能多得10分,, ...

  5. org.omg.CORBA.MARSHAL: vmcid: SUN minor code: 211 completed: Maybe

    用weblogic 12c 测试 ejb3 import javax.naming.InitialContext; import javax.naming.NamingException; impor ...

  6. 看好腾讯,鄙视百度(腾讯的核心竞争力,不是超过10亿的QQ的注册用户,也不是某一项产品、技术方面优势,而是“耐心”:懂得在合适的时间推出合适的产品。”)

    百度,自始至终只是一个低劣的模仿者,且一切向前看,完全违背了一个搜索引擎所应该遵循的基本原则.谁给的钱多就能搜着谁,这跟贩毒有什么区别? 腾讯也在模仿别人,但是,它是模仿然后超越.在中国互联网发展历史 ...

  7. 1.一个WEB应用的开发流程

    先说项目开发过程中团队人员的分工协作. 一.人员安排 毕业至今的大部分项目都是独立完成,虽然也有和其他同事协作的时候,但自认为对团队协作的了解和认知都还有所欠缺.很清楚团队协作的重要性,但尚未有很好的 ...

  8. ORACLE10g R2【RAC+ASM→单实例FS】

    ORACLE10g R2[RAC+ASM→单实例FS] 10g R2 RAC+ASMà单实例FS的DG,建议禁用OMF. 本演示案例所用环境:   primary standby OS Hostnam ...

  9. [D3] Drawing path in D3

    Here we have a force layout with three nodes. In the example, we will link three nodes with line and ...

  10. !!在JS中代表什么

    !!一般用来将后面的表达式转换为布尔型的数据(boolean), javascript约定和c类似,规则为 ·false.undefinded.null.0."" 为 false, ...