本篇內(nèi)容介紹了“微信小程序怎么實現(xiàn)計算器”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

效果如下:

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: "" })
}
})“微信小程序怎么實現(xiàn)計算器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質量的實用文章!
網(wǎng)站欄目:微信小程序怎么實現(xiàn)計算器-創(chuàng)新互聯(lián)
本文URL:http://www.jbt999.com/article42/dgjihc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、面包屑導航、網(wǎng)站設計公司、網(wǎng)站策劃、網(wǎng)站內(nèi)鏈、手機網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)