Vue實(shí)現(xiàn)手機(jī)計(jì)算器
本文實(shí)例為大家分享了Vue制作仿手機(jī)計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
1.首先是把樣式做出來,按鈕是0-9,還有加減乘除,百分號(hào),清除按鈕,小數(shù)點(diǎn),等號(hào)、等等
2.把官方網(wǎng)站的JS插件引用,cn.vuejs.org/v2/guide/
頁面視圖
JS
new Vue({ el: '#app', data: { equation: ’0’, isDecimalAdded: false, //防止在一組數(shù)字中間輸入超過一個(gè)小數(shù)位 isOperatorAdded: false, //判斷之否已點(diǎn)擊 加、減、乘、除,防止連續(xù)點(diǎn)擊超過一個(gè)運(yùn)算符號(hào) isStarted: false, //判斷計(jì)算器是否已經(jīng)開始輸入數(shù)字,用于正負(fù)數(shù)和百分比計(jì)算的時(shí)候作一些判斷 }, methods: { //Check if the character is + - × ÷ isOperator(character) { //用來判斷character 是否加減乘除 return [’+’, ’-’, ’×’, ’÷’].indexOf(character) > -1 }, append(character) { //append(character)加減乘除 if (this.equation === ’0’ && !this.isOperator(character)) { if (character === ’.’) { this.equation += ’’ + character this.isDecimalAdded = true } else { this.equation = ’’ + character } this.isStarted = true return } if (!this.isOperator(character)) { if (character === ’.’ && this.isDecimalAdded) { return } if (character === ’.’) { this.isDecimalAdded = true this.isOperatorAdded = true } else { this.isOperatorAdded = false } this.equation += ’’ + character } if (this.isOperator(character) && !this.isOperatorAdded) { this.equation += ’’ + character this.isDecimalAdded = false this.isOperatorAdded = true } }, calculate() { //等于號(hào)的時(shí)候 let result = this.equation.replace(new RegExp(’×’, ’g’), ’*’).replace(new RegExp(’÷’, ’g’), ’/’) this.equation = parseFloat(eval(result).toFixed(9)).toString() this.isDecimalAdded = false this.isOperatorAdded = false }, calculateToggle() { //點(diǎn)擊正負(fù)號(hào) if (this.isOperatorAdded || !this.isStarted) { true } this.equation = this.equation + ’* -1’ this.calculate() }, calculatePercentage() { //點(diǎn)擊百分比 if (this.isOperatorAdded || !this.isStarted) { true } this.equation = this.equation + ’* 0.01’ this.calculate() }, clear() { //點(diǎn)擊AC this.equation = ’0’ this.isDecimalAdded = false //防止在一組數(shù)字中間輸入超過一個(gè)小數(shù)位 this.isOperatorAdded = false //判斷之否已點(diǎn)擊 加、減、乘、除,防止連續(xù)點(diǎn)擊超過一個(gè)運(yùn)算符號(hào) this.isStarted = false //判斷計(jì)算器是否已經(jīng)開始輸入數(shù)字,用于正負(fù)數(shù)和百分比計(jì)算的時(shí)候作一些判斷 } }})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
