微信小程序計算器實例詳解
更新時間:2021年07月19日 09:25:43 作者:yfy538
這篇文章主要為大家詳細(xì)介紹了微信小程序計算器實例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
微信小程序計算器實例,供大家參考,具體內(nèi)容如下

index.wxml
<view class="content">
<view class="num">{{num}}</view>
<view class="operotor">{{op}}</view>
</view>
<view class="entry">
<view>
<view class="item" bindtap="resetBtn">c</view>
<view class="item" bindtap="delBtn">DEL</view>
<view class="item" bindtap="opBtn" data-val="%">%</view>
<view class="item" bindtap="opBtn" data-val="/">÷</view>
</view>
<view>
<view class="item" bindtap="numBtn" data-val="7">7</view>
<view class="item" bindtap="numBtn" data-val="8">8</view>
<view class="item" bindtap="numBtn" data-val="9">9</view>
<view class="item" bindtap="opBtn" data-val="*">x</view>
</view>
<view>
<view class="item" bindtap="numBtn" data-val="4">4</view>
<view class="item" bindtap="numBtn" data-val="5">5</view>
<view class="item" bindtap="numBtn" data-val="6">6</view>
<view class="item" bindtap="opBtn" data-val="-">-</view>
</view>
<view>
<view class="item" bindtap="numBtn" data-val="1">1</view>
<view class="item" bindtap="numBtn" data-val="2">2</view>
<view class="item" bindtap="numBtn" data-val="3">3</view>
<view class="item" bindtap="opBtn" data-val="+">+</view>
</view>
<view>
<view class="item tow" bindtap="numBtn" data-val="0">0</view>
<view class="item one" bindtap="dotBtn" data-val=".">.</view>
<view class="item one" bindtap="opBtn" data-val="=">=</view>
</view>
</view>
index.css
page {
display: flex;
flex-direction: column;
height: 100%;
}
.content {
flex: 1;
background-color: #f3f6fe;
position: relative;
}
.content .num {
position: absolute;
font-size: 27pt;
bottom: 5vh;
right: 3vw;
}
.content .operotor {
font-size: 15pt;
position: absolute;
bottom: 1vh;
right: 3vw;
}
.entry {
flex: 1;
font-size: 17pt;
border-top: 1rpx solid #ccc;
}
.entry .item {
flex: 1;
padding: 30rpx 0;
text-align: center;
flex-basis: 25%;
border-left: 1rpx solid #ccc;
border-bottom: 1rpx solid #ccc;
}
.entry > view {
display: flex;
}
.entry > view .tow {
flex: 2;
}
.entry > view .one {
flex: 1;
}
index.js
Page({
data: {
num: "", // 存儲數(shù)字
op: "" //存儲運算符
},
result: null,
isClear: false,
numBtn: function(e) {
var num = e.target.dataset.val
//console.log(num) 得到data-val的值
console.log(this.isClear)
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)
this.setData({ op: e.target.dataset.val })
if (this.isClear) {
return
}
this.isClear = true
if (this.result === null) {
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
} 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 num = this.data.num.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: "" })
}
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
three.js顯示中文字體與tween應(yīng)用詳析
這篇文章主要給大家介紹了關(guān)于three.js顯示中文字體與tween應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
JavaScript對象的特性與實踐應(yīng)用深入詳解
這篇文章主要介紹了JavaScript對象的特性與實踐應(yīng)用,結(jié)合實例形式較為深入的分析了javascript對象的相關(guān)概念、操作方法及注意事項,需要的朋友可以參考下2018-12-12
JavaScript 字符串處理函數(shù)使用小結(jié)
JavaScript 字符串處理函數(shù)使用小結(jié),學(xué)習(xí)js的朋友可以參考下。2010-12-12
淺析AMD CMD CommonJS規(guī)范--javascript模塊化加載學(xué)習(xí)心得總結(jié)
下面小編就為大家分享一篇淺析AMD CMD CommonJS規(guī)范--javascript模塊化加載學(xué)習(xí)心得總結(jié)。小編覺得寫的非常不錯,需要的朋友可以過來參考一下2016-03-03
原生JS版和jquery版實現(xiàn)checkbox的全選/全不選/點選/行內(nèi)點選(Mr.Think)
腳本之家小編之前整理不少checkbox全選全不選這方便的文章,但看了這篇以后發(fā)現(xiàn)實現(xiàn)方法更好2016-10-10

