- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- 微信小程序實(shí)現計算器功能
本文實(shí)例為大家分享了微信小程序實(shí)現計算器功能的具體代碼,供大家參考,具體內容如下
第一次進(jìn)到頁(yè)面它的目錄結構如下:
(1)添加的新頁(yè)面文件,都需要在app.json中進(jìn)行配置,否則頁(yè)面報錯。
(2)工作原理 通過(guò)在<view></view>中添加事件 bindtap="btnClick" id="{{n9}}" 相當于click事件。
在js代碼中,可以通過(guò)this.data.n9獲取數據,這些數據的定義都是在js中
通過(guò)在<view id="{{btn_a}}"><view>填寫(xiě)id,在具體的函數中,event.target.id去判斷id是多少,進(jìn)行區分。就可以實(shí)現,不同標簽的點(diǎn)擊,然后進(jìn)行業(yè)務(wù)邏輯。如果需要訪(fǎng)問(wèn)數據,則是通過(guò)this.data.xx。
計算器的wxml頁(yè)面
<view class="content"> <view class="xianshi">{{screenNum}}</view> <view class="anniu"> <view class="item blue" bindtap="btnClick" id="{{n9}}">9</view> <view class="item blue" bindtap="btnClick" id="{{n8}}">8</view> <view class="item blue" bindtap="btnClick" id="{{n7}}">7</view> <view class="item blue" bindtap="btnClick" id="{{na}}">+</view> </view> <view class="anniu"> <view class="item blue" bindtap="btnClick" id="{{n6}}">6</view> <view class="item blue" bindtap="btnClick" id="{{n5}}">5</view> <view class="item blue" bindtap="btnClick" id="{{n4}}">4</view> <view class="item blue" bindtap="btnClick" id="{{nb}}">-</view> </view> <view class="anniu"> <view class="item blue" bindtap="btnClick" id="{{n3}}">3</view> <view class="item blue" bindtap="btnClick" id="{{n2}}">2</view> <view class="item blue" bindtap="btnClick" id="{{n1}}">1</view> <view class="item blue" bindtap="btnClick" id="{{nc}}">*</view> </view> <view class="anniu"> <view class="item blue" bindtap="btnClick" id="{{n0}}">0</view> <view class="item blue" bindtap="btnClear">AC</view> <view class="item blue" bindtap="btnJs">=</view> <view class="item blue" bindtap="btnClick" id="{{nd}}">/</view> </view> </view>
// pages/cal/cal.js Page({ /** * 頁(yè)面的初始數據 */ data: { n0: 0, n1: 1, n2: 2, n3: 3, n4: 4, n5: 5, n6: 6, n7: 7, n8: 8, n9: 9, na: '+', nb: '-', nc: '*', nd: '/', screenNum: 0, screenStr: 0, is_num:1 }, /** * 生命周期函數--監聽(tīng)頁(yè)面加載 */ onLoad: function (options) { }, /** * 生命周期函數--監聽(tīng)頁(yè)面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數--監聽(tīng)頁(yè)面顯示 */ onShow: function () { }, /** * 生命周期函數--監聽(tīng)頁(yè)面隱藏 */ onHide: function () { }, /** * 生命周期函數--監聽(tīng)頁(yè)面卸載 */ onUnload: function () { }, /** * 頁(yè)面相關(guān)事件處理函數--監聽(tīng)用戶(hù)下拉動(dòng)作 */ onPullDownRefresh: function () { }, /** * 頁(yè)面上拉觸底事件的處理函數 */ onReachBottom: function () { }, /** * 用戶(hù)點(diǎn)擊右上角分享 */ onShareAppMessage: function () { }, btnClick:function(event){ //console.log('你按得鍵是'+event.target.id); //console.log('上一次' + this.data.is_num); var op=''; var data=0; var last_is_num = this.data.is_num; //這次輸入的是什么 if (event.target.id == '9' || event.target.id == '8' || event.target.id == '7' || event.target.id == '6' || event.target.id == '5' || event.target.id == '4' || event.target.id == '3' || event.target.id == '2' || event.target.id == '1' || event.target.id == '0') { data = event.target.id; this.setData({ is_num: 1 }); } if (event.target.id == '+' || event.target.id == '-' || event.target.id == '*' || event.target.id == '/') { op = event.target.id; this.setData({ is_num: 0 }); } if (last_is_num==1){ //如果上一次是數字 if (op == ''){ //這一次是數字 if (this.data.screenNum!=0){ this.setData({ screenNum: this.data.screenNum + data }); this.setData({ screenStr: this.data.screenStr + data }); }else{ this.setData({ screenNum: data}); this.setData({ screenStr: data }); } }else{ this.setData({ screenNum: this.data.screenNum + op }); this.setData({ screenStr: this.data.screenStr +',' +op+',' }); } }else{ //上次不是數字 if (data != 0) { //這一次是數字 this.setData({ screenNum: this.data.screenNum + data }); this.setData({ screenStr: this.data.screenStr + data }); } else { return; } } //console.log(op+'aaaaa'+data); //console.log('現在是'+this.data.is_num); //console.log('screenNum' + this.data.screenNum); //console.log(this.data.screenStr); }, btnJs:function(){ console.log(this.data.screenNum); console.log(this.data.screenStr); var result=0; var strs = new Array(); //定義一數組 strs = this.data.screenStr.split(","); //字符分割 for (var i = 0; i < strs.length; i++) { //console.log(strs[i] + i); //分割后的字符輸出 if (strs[i]=='+'){ result = parseInt(strs[i - 1]) + parseInt(strs[i+1]); } if (strs[i] == '-') { result = strs[i - 1] - strs[i + 1]; } if (strs[i] == '*') { result = strs[i - 1] * strs[i + 1]; } if (strs[i] == '/') { result = strs[i - 1] / strs[i + 1]; } } console.log('result:'+result); this.setData({ screenNum: result}); this.setData({ screenStr: result }); }, btnClear:function(){ //把標記恢復成默認狀態(tài) this.setData({ screenNum: 0 }); this.setData({ screenStr: 0 }); this.setData({ is_num: 1 }); } })
總結,在小程序的布局方面引入了相對單位rpx,需要在學(xué)習一下彈性盒子flex布局。對于js部分,和vue.js有些類(lèi)似,都是對數據進(jìn)行綁定,簡(jiǎn)化js的dom操作。這兩點(diǎn)還是需要再看看。
以上就是本文的全部?jì)热?,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站