棉花糖 发表于 2018-4-30 11:48:29

技术干货:微信小程序开发简易计算器改进版


微信小程序开发计算器有多种方法,但是大部分代码比较复杂、不容易理解。本案例进行了改进,主要是组件bindtap属性绑定的自定义函数clickBtn(),采用了switch语句,使得代码结构更加清晰,学习者更容易理解,现分享如下。案例的效果如下图所示:
http://p3.pstatp.com/large/59300003c52d57173985
制作步骤如下:
1. 在微信开发者工具的全局控制app.json文件pages数组中输入“pages/calculator/calcu”,建立计算器页面相关文件,在window属性中更改导航标题为“简单计算器”。app.json的代码如下:
{
"pages":[
"pages/calculator/calcu",
"pages/index/index",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "简单计算器",
"navigationBarTextStyle":"black"
}
}
2. 页面布局文件calcu.wxml的代码如下:
{{screenData}}

C
+/-
+
9
8
7
-
6
5
4
×
3
2
1
÷
0
.

=
3. 页面样式文件calcu.wxss的代码如下:
/* */
.content{
height:100%;
display: flex;
flex-direction: column;
align-items:center;
box-sizing: border-box;
background:#555;
padding:30rpx 0 20rpx 0;
}
.scr{
background:#fff;
width:700rpx;
height: 100rpx;
line-height: 100rpx;
text-align: right;
margin:0 0 10rpx 0;
padding:0 10rpx 0 0;
font-size:30px;
}
.btnGroup{
display: flex;
flex-direction: row;
}
.item{
width:175rpx;
min-height: 150rpx;
margin: 1rpx;
text-shadow:0 1px 1px rgba(255,255,255,.3);
text-align: center;
line-height: 150rpx;
}
.gray{
background:#eee;
}
.orange{
color:#fff;
background:#f60;
}
4.逻辑处理文件calcu.js的代码如下:
Page({
data: {
b01:"back",
b02: "clear",
b03: "negative",
b04: "+",
b05: "9",
b06: "8",
b07: "7",
b08: "-",
b09: "6",
b10: "5",
b11: "4",
b12: "×",
b13: "3",
b14: "2",
b15: "1",
b16: "÷",
b17: "0",
b18: ".",
b19: "sqrt",
b20: "=" ,
screenData:"0",
lastOperator:false,
arr:[],
logs:[]
},
clickBtn: function (event) {
console.log(event.target.id);
var id = event.target.id;
switch (id) {
case "back":
//退格
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();
break;
case "clear":
//清屏
this.setData({
screenData: "0"
});
this.data.arr.length = 0;
break;
case "negative":
//正负号
var data = this.data.screenData;
if (data == 0) {
return;
}
var firstword = data.substring(0, 1);
if (firstword == "-") {
data = data.substring(1, data.length);
this.data.arr.shift();
} else {
data = "-" + data;
this.data.arr.unshift("-");
}
this.setData({
screenData: data
});
break;
case "=":
//等号=
var data = this.data.screenData;
if (data == 0) {
return;
}
var lastWord = data.substring(data.length - 1, 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) == false || arr == this.data.b18 || arr == this.data.b03) {
num += arr;
} else {
lastOperator = arr;
optarr.push(num);
optarr.push(arr);
num = "";
}
}
optarr.push(Number(num));
var result = Number(optarr) * 1.0;
console.log(result);
for (var i = 1; i
if (isNaN(optarr)) {
if (optarr == this.data.b04) {
result += Number(optarr);
} else if (optarr == this.data.b08) {
result -= Number(optarr);
} else if (optarr == this.data.b12) {
result *= Number(optarr);
} else if (optarr == this.data.b16) {
result /= Number(optarr);
}
}
}
//
this.data.arr.length = 0;
this.data.arr.push(result);
this.setData({
screenData: result + ""
});
break;
default:
if (id == this.data.b04 || id == this.data.b08 || id == this.data.b12 || id == this.data.b16) {
if (this.data.lastOperator == true || this.data.screenData == 0) {
return;
}
}
var num = this.data.screenData;
var data;
if (num == 0) {
data = id;
} else {
data = num + id;
}
this.setData({
screenData: data
});
this.data.arr.push(id);
if (id == this.data.b04 || id == this.data.b08 || id == this.data.b12 || id == this.data.b16) {
this.setData({
lastOperator: true
});
} else {
this.setData({
lastOperator: false
});
}
break;
}
},
getSqrt: function () {
//平方根
var data = Math.sqrt(this.data.screenData);
this.setData({
screenData: data
});
}
})
5. 至此,案例制作完成,希望对大家的学习有很好的帮助。
页: [1]
查看完整版本: 技术干货:微信小程序开发简易计算器改进版

请大家牢记南瓜园网址 www.nan2008.com