微信小程序?qū)崿F(xiàn)計(jì)算器(含歷史記錄)
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
1、實(shí)現(xiàn)效果圖
2、代碼編寫
index.wxml
<!--author:LHXZ 靈魂學(xué)者--> <!--pages/index/index.wxml--> <view class="result"> ? <view class="historyed">|歷史記錄</view> ? <scroll-view scroll-y bindscrolltolower class="historying"> ? ? ? ? <view wx:for="{{logs}}" wx:key="item" class="h_text">{{item}}</view> ? </scroll-view> ? <view class="clear" bindtap="clear">清空記錄|</view> ? <view class="history">{{history}}</view> ? <view class="result-num">{{num}}</view> ? <view class="result-op">{{op}}</view> </view> <view class="btns"> ? <view> ? ? <view hover-class="bg" bindtap="resetBtn">C</view> ? ? <view hover-class="bg" bindtap="delBtn">DEL</view> ? ? <view hover-class="bg" bindtap="opBtn" data-val="%">%</view> ? ? <view hover-class="bg" bindtap="opBtn" data-val="/">÷</view> ? </view> ? <view> ? ? <view hover-class="bg" bindtap="numBtn" data-val="7">7</view> ? ? <view hover-class="bg" bindtap="numBtn" data-val="8">8</view> ? ? <view hover-class="bg" bindtap="numBtn" data-val="9">9</view> ? ? <view hover-class="bg" bindtap="opBtn" data-val="*">×</view> ? </view> ? <view> ? ? <view hover-class="bg" bindtap="numBtn" data-val="4">4</view> ? ? <view hover-class="bg" bindtap="numBtn" data-val="5">5</view> ? ? <view hover-class="bg" bindtap="numBtn" data-val="6">6</view> ? ? <view hover-class="bg" bindtap="opBtn" data-val="-">-</view> ? </view> ? <view> ? ? <view hover-class="bg" bindtap="numBtn" data-val="1">1</view> ? ? <view hover-class="bg" bindtap="numBtn" data-val="2">2</view> ? ? <view hover-class="bg" bindtap="numBtn" data-val="3">3</view> ? ? <view hover-class="bg" bindtap="opBtn" data-val="+">+</view> ? </view> ? <view> ? ? <view hover-class="bg" bindtap="numBtn" data-val="0">0</view> ? ? <view hover-class="bg" bindtap="dotBtn">.</view> ? ? <view hover-class="bg" bindtap="opBtn" data-val="=">=</view> ? </view> </view>
index.wxss
/* pages/index/index.wxss ?author:LHXZ 靈魂學(xué)者*/ page { ? display: flex; ? flex-direction: column; ? height: 100%; ? color: #555; } ? .result { ? flex: 1; ? background: #f3f6fe; ? position: relative; } ? .result-num { ? position: absolute; ? font-size: 27pt; ? bottom: 5vh; ? right: 3vw; } ? .result-op { ? font-size: 15pt; ? position: absolute; ? bottom: 1vh; ? right: 3vw; } ? .btns { ? flex: 1; } ? /* 按鈕樣式 */ ? .bg { ? background: #eee; } ? .btns { ? flex: 1; ? display: flex; ? flex-direction: column; ? font-size: 17pt; ? border-top: 1rpx solid #ccc; ? border-left: 1rpx solid #ccc; } ? .btns > view { ? flex: 1; ? display: flex; } ? .btns > view > view { ? flex-basis: 25%; ? border-right: 1rpx solid #ccc; ? border-bottom: 1rpx solid #ccc; ? box-sizing: border-box; ? display: flex; ? align-items: center; ? justify-content: center; } ? .btns > view:last-child > view:first-child { ? flex-basis: 50%; } ? .btns > view:first-child > view:first-child { ? color: #f00; } ? .btns > view > view:last-child { ? color: #fc8e00; } .history{ ? position: absolute; ? top:20rpx; ? right:40rpx; ? font-size: 2rem; ? color: rgb(199, 199, 199); } .h_text{ ? color: rgb(136, 136, 136); ? margin-left: 20rpx; } .historying{ ? position: absolute; ? top:200rpx; } scroll-view{ ? height: 200rpx; ? background-color: rgba(255, 255, 255, 0.5); } .historyed{ ? position: absolute; ? top:150rpx; ? left: 10rpx; ? font-size: 30rpx; ? color: gray; } .clear{ ? position: absolute; ? top:150rpx; ? right: 10rpx; ? font-size: 30rpx; ? color: blue; }
index.json
{ ? "navigationBarBackgroundColor": "#fff", ? "navigationBarTitleText": "計(jì)算器", ? "navigationBarTextStyle": "black", ? "usingComponents": { ? } }
index.js
/*author:LHXZ 靈魂學(xué)者*/ Page({ ? /** ? ?* 頁面的初始數(shù)據(jù) ? ?*/ ? data: { ? ? num: '0', ? ? op: '', ? ? history:'', ? ? logs:[] ? }, ? result: null, ? isClear: false, ? ? // 數(shù)字按鈕事件處理函數(shù) ? numBtn: function(e) { ? ? var num = e.target.dataset.val? ? ? if (this.data.num === '0' || this.isClear) { ? ? ? this.setData({ ? ? ? ? num:num ? ? ? })? ? ? this.isClear = false ? ? } else{ ? ? ? this.setData({ ? ? ? ? num: this.data.num + num ? ? ? }) ? ? } ? ? ? ?? ? }, ? ? // 運(yùn)算符事件處理函數(shù) ? opBtn: function(e) { ? ? var op = this.data.op; ? ? var num = Number(this.data.num); ? ? var newOp = e.target.dataset.val ? ? this.setData({ ? ? ? op:newOp ? ? }) ? ? ? if (this.isClear) { ? ? ? this.setData({ ? ? ? ? history:this.data.history.substr(0,this.data.history.length-1)+newOp ? ? ? }) ? ? ? return ? ? } ? ? this.setData({history:this.data.history+''+num+newOp}) ? ? 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 ? ? }else if ?(op === '=') { ? ? ? this.result =num ? ? ? this.setData({ ? ? ? ? history:num + newOp ? ? ? }) ? ? } ? ? ? this.setData({ ? ? ? num: this.result + '', ? ? })? ? ? // 歷史記錄 ? ? this.data.logs.unshift(this.data.history); ? ? this.setData({ ? ? ? logs:this.data.logs ? ? }) ? }, ? ?? ? // 清空記錄 ? clear:function(){ ? ? this.setData({ ? ? ? logs:[] ? ? }) ? }, ?? ? // 小數(shù)點(diǎn)事件處理函數(shù) ? 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 + '.' ? ? }) ? }, ? // DEL按鈕處理函數(shù) ? delBtn: function() { ? ? var num = this.data.num.substr(0, this.data.num.length - 1) ? ? this.setData({ ? ? ? num: num === '' ? '0' : num ? ? }) ? }, ? ? // C按鈕事件處理函數(shù) ? resetBtn: function() { ? ? this.result = null ? ? this.isClear = false ? ? this.setData({ ? ? ? num: '0', ? ? ? op: '', ? ? ? history:'' ? ? }) ? } })
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ant-design-pro使用qiankun微服務(wù)配置動(dòng)態(tài)主題色的問題
這篇文章主要介紹了ant-design-pro使用qiankun微服務(wù)配置動(dòng)態(tài)主題色,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03js中用window.open()打開多個(gè)窗口的name問題
這篇文章主要介紹了js中用window.open()打開多個(gè)窗口的問題,需要的朋友可以參考下2014-03-03uniapp實(shí)現(xiàn)人臉識(shí)別功能的具體實(shí)現(xiàn)代碼
最近在使用uniapp開發(fā)項(xiàng)目,有刷臉實(shí)名認(rèn)證的需求,下面這篇文章主要給大家介紹了關(guān)于uniapp實(shí)現(xiàn)人臉識(shí)別功能的具體實(shí)現(xiàn),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12JS碰撞運(yùn)動(dòng)實(shí)現(xiàn)方法詳解
這篇文章主要介紹了JS碰撞運(yùn)動(dòng)實(shí)現(xiàn)方法,詳細(xì)分析了碰撞運(yùn)動(dòng)的原理及相應(yīng)的javascript實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-12-12幫你徹底搞懂JS中的prototype、__proto__與constructor(圖解)
這篇文章主要介紹了詳解幫你徹底搞懂JS中的prototype、__proto__與constructor(圖解),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08細(xì)數(shù)localStorage的用法及使用注意事項(xiàng)
這篇文章主要介紹了細(xì)數(shù)localStorage的用法及使用注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04