国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue.js實(shí)現(xiàn)的計(jì)算器功能完整示例

2019-11-19 13:30:44
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Vue.js實(shí)現(xiàn)的計(jì)算器功能。分享給大家供大家參考,具體如下:

1. HTML部分代碼

<!DOCTYPE html><html lang="en"><head>  <link rel="stylesheet" type="text/css" href="css/css.css" rel="external nofollow" >  <script type="text/javascript" src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>  <meta charset="UTF-8">  <title>my-calculator</title></head><body><div id="calculator">  <!--顯示框-->  <input-box v-bind:input-show="inputShow">  </input-box>  <btn-list>    <div @click="clearValue()" class=" btn-30 btn-radius color-red clear-marginleft">C</div>    <div class=" btn-30 btn-radius color-blue">+/-</div>    <div @click="inputValue('%')" class=" btn-30 btn-radius color-blue">%</div>    <div @click="backValue()" class=" btn-70 btn-radius color-red font-14">←</div>    <div @click="inputValue('7')" class=" btn-30 btn-radius clear-marginleft">7</div>    <div @click="inputValue('8')" class=" btn-30 btn-radius">8</div>    <div @click="inputValue('9')" class=" btn-30 btn-radius">9</div>    <div @click="squareValue()" class=" btn-30 btn-radius color-blue font-14">ײ</div>    <div @click="radicalValue()" class=" btn-30 btn-radius color-blue font-12">√</div>    <div @click="inputValue('4')" class=" btn-30 btn-radius clear-marginleft">4</div>    <div @click="inputValue('5')" class=" btn-30 btn-radius">5</div>    <div @click="inputValue('6')" class=" btn-30 btn-radius">6</div>    <div @click="inputValue('×')" class=" btn-30 btn-radius color-blue font-14">×</div>    <div @click="inputValue('÷')" class=" btn-30 btn-radius color-blue font-12">÷</div>    <div @click="inputValue('1')" class=" btn-30 btn-radius clear-marginleft">1</div>    <div @click="inputValue('2')" class=" btn-30 btn-radius">2</div>    <div @click="inputValue('3')" class=" btn-30 btn-radius">3</div>    <div @click="inputValue('+')" class=" btn-30 btn-radius color-blue font-14">+</div>    <div @click="inputValue('-')" class=" btn-30 btn-radius color-blue font-14">-</div>    <div @click="inputValue('0')" class=" btn-70 btn-radius clear-marginleft">0</div>    <div @click="inputValue('.')" class=" btn-30 btn-radius">.</div>    <div @click="calValue()" class=" btn-70 btn-radius color-red font-14">=</div>  </btn-list></div><script>var calculator = new Vue({  el:'#calculator',  data:{    inputShow:{      value:'0'    }  },  components:{    'input-box':{      props:['inputShow'],      computed: {        value:function () {          return this.inputShow.value        }      },      template:'<input id="input-box" type="text" size="21" maxlength="21" v-model="value" readonly="readonly">'    },    'btn-list':{      template:'<div id="btn-list"><slot></slot></div>'    }  },  methods:{    inputValue(param){      if(Object.prototype.toString.call(this.inputShow.value) == "[object Number]"){   //判斷輸入框內(nèi)容是否為數(shù)字類型        this.inputShow.value = "0";   //數(shù)字類型說明是上個(gè)計(jì)算結(jié)果,清空內(nèi)容      }      var str ='' + this.inputShow.value; //輸入內(nèi)容時(shí),將輸入框內(nèi)容轉(zhuǎn)為字符串類型      var len = str.length;      var arr = ["+","-","×","÷"];      var num = (''+parseFloat(str.split('').reverse().join(''))).split('').reverse().join('');   //parseInt(str.split('').reverse().join('')))是獲取輸入框內(nèi)最后一串?dāng)?shù)字,再反轉(zhuǎn)回來 ,num為輸入框內(nèi)最后一串?dāng)?shù)字      var nlen = num.length;      if((num!= '0' && param != '.')|| (param == '.'&& num.indexOf(".")==-1)){   //輸入框內(nèi)最后一串?dāng)?shù)字不為0時(shí)拼接字符串        if(arr.indexOf(str.charAt(len-1)) != -1 && arr.indexOf(param) != -1){    //若一開始輸入內(nèi)容為運(yùn)算符,輸入無效          return;        }        this.inputShow.value += param;   //拼接輸入內(nèi)容      }else{        arr.push("%");        if(param == '.'){      //若num中已有小數(shù)點(diǎn),輸入內(nèi)容為小數(shù)點(diǎn),視為無效          return;        }else if(!(arr.indexOf(param) != -1)){    //判斷輸入框內(nèi)最后一個(gè)字符不為運(yùn)算符          this.inputShow.value =str.substring(0,str.length-nlen) + param;  //輸入框內(nèi)最后一串?dāng)?shù)字為0時(shí),刪除0拼接        }      }    },    clearValue(){      //清空輸入框內(nèi)容      this.inputShow.value = '0';    },    calValue(){       //計(jì)算結(jié)果      var str = this.inputShow.value;      str = str.replace('×','*').replace('÷','/').replace('%','*0.01');    //替換運(yùn)算符      try{        this.inputShow.value = eval(str);      //若用戶輸入內(nèi)容不符合運(yùn)算規(guī)則,不計(jì)算      }catch(error){        return;      }    },    squareValue(){             //平方計(jì)算      var str = this.inputShow.value;      this.inputShow.value = Math.pow(eval(str),2)    },    radicalValue(){             //開根號(hào)計(jì)算      var str = this.inputShow.value;      this.inputShow.value = Math.sqrt(eval(str));    },    backValue(){              //刪除鍵,刪除單個(gè)字符      var str = this.inputShow.value;      if(str.length == 1){        this.inputShow.value = "0";      }else{         this.inputShow.value = str.slice(0,str.length-1);      }    },    /*oppositeValue(){            //正負(fù)號(hào)取值      var str = this.inputShow.value;      var num = (''+parseInt(str.split('').reverse().join(''))).split('').reverse().join('');   //獲取輸入框內(nèi)最后遺傳數(shù)字      var nlen = num.length;      debugger;      if(!isNaN( parseInt(str.charAt(str.length-1))) && num != 0){  //當(dāng)輸入框末位字符為數(shù)字且最后一串?dāng)?shù)字不為0時(shí),取正負(fù)        this.inputShow.value = str.substring(0,str.length-nlen)+`(-${num})`;      }    }*/  }})</script></body></html>

2. CSS部分代碼

@charset "utf-8";body, ul, dl, dd, dt, ol, li, p, h1, h2, h3, h4, h5, h6, textarea, form, select, fieldset, table, td, div, input{margin:0;padding:0;-webkit-text-size-adjust:none}h1, h2, h3, h4, h5, h6{font-size:12px;font-weight:normal}body>div{margin:0 auto}div{text-align:left}a img{border:0}body{color:#333;text-align:center;font:12px "微軟雅黑";}ul, ol, li{list-style-type:none;vertical-align:0}a{outline-style:none;color:#535353;text-decoration:none}a:hover{color:#D40000;text-decoration:none}.clear{height:0;overflow:hidden;clear:both}/* calculator */#calculator{width:200px;height:245px;padding:10px;border:1px solid #e5e5e5;background:#f8f8f8;-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;box-shadow:0px 0px 10px #f2f2f2;-moz-box-shadow:0px 0px 10px #f2f2f2;-webkit-box-shadow:0px 0px 10px #f2f2f2;margin:40px auto 0 auto;}#calculator #input-box{margin:0;width:187px;padding:9px 5px;height:14px;border:1px solid #e5e5e5;border-radius:3px;-webkit-border-radius:3px;-moz-border-radius:3px;background:#FFF;text-align:right;line-height:14px;font-size:12px;font-family:Verdana, Geneva, sans-serif;color:#666;outline:none; text-transform:uppercase;}#calculator #btn-list{width:200px;overflow:hidden;}#calculator #btn-list .btn-radius{border-radius:2px;-webkit-border-radius:2px;-moz-border-radius:2px;border:1px solid #e5e5e5;background:-webkit-gradient(linear, 0 0, 0 100%, from(#f7f7f7), to(#ebebeb));background:-moz-linear-gradient(top, #f7f7f7,#ebebeb);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#f7f7f7,endColorstr=#ebebeb,grandientType=1);line-height:29px;text-align:center;text-shadow:0px 1px 1px #FFF;font-weight:bold;font-family:Verdana, Geneva, sans-serif;color:#666;float:left;margin-left:11px;margin-top:11px;font-size:12px;cursor:pointer;}#calculator #btn-list .btn-radius:active{background:#ffffff;}#calculator #btn-list .clear-marginleft{margin-left:0;}#calculator #btn-list .font-14{font-size:14px;}#calculator #btn-list .color-red{color:#ff5050}#calculator #btn-list .color-blue{color:#00b4ff}#calculator #btn-list .btn-30{width:29px;height:29px;}#calculator #btn-list .btn-70{width:70px;height:29px;}

3. 使用本站HTML/CSS/JS在線運(yùn)行測(cè)試工具http://tools.VeVB.COm/code/HtmlJsRun,可得到如下測(cè)試運(yùn)行效果:

4. 使用時(shí)記得改下css路徑,在html中引入vue

5. 博主技術(shù)有限,正負(fù)號(hào)部分功能還有問題待完善.計(jì)算器還有一些未知的小Bug,感興趣的讀者可以在這個(gè)基礎(chǔ)上進(jìn)行擴(kuò)展。

PS:這里再為大家推薦幾款計(jì)算工具供大家參考:

在線數(shù)學(xué)表達(dá)式簡(jiǎn)單轉(zhuǎn)換/計(jì)算工具:
http://tools.VeVB.COm/jisuanqi/exp_jisuanqi

在線一元函數(shù)(方程)求解計(jì)算工具:
http://tools.VeVB.COm/jisuanqi/equ_jisuanqi

科學(xué)計(jì)算器在線使用_高級(jí)計(jì)算器在線計(jì)算:
http://tools.VeVB.COm/jisuanqi/jsqkexue

在線計(jì)算器_標(biāo)準(zhǔn)計(jì)算器:
http://tools.VeVB.COm/jisuanqi/jsq

希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 和政县| 霍林郭勒市| 瑞金市| 澄城县| 磴口县| 县级市| 固原市| 英超| 化州市| 宁德市| 读书| 黄骅市| 汪清县| 满洲里市| 舞钢市| 蓬莱市| 壤塘县| 德江县| 太和县| 井研县| 鹿邑县| 海南省| 防城港市| 岐山县| 浪卡子县| 西充县| 翁源县| 柳河县| 荥阳市| 尼木县| 凤山市| 陈巴尔虎旗| 灵丘县| 绥中县| 平陆县| 玉溪市| 呼伦贝尔市| 宜宾市| 蒙阴县| 上林县| 陇川县|