vue.js实现的经典计算器/科学计算器功能示例


Posted in Javascript onJuly 11, 2018

本文实例讲述了vue.js实现的经典计算器/科学计算器功能。分享给大家供大家参考,具体如下:

1. HTML部分:

<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<div id="app">
 <div class="calculator">
  <button @click="changeModeEvent" class="toggle-button">
   <p v-if="changeMode">Show Advanced Mode   ⚈</p>
   <p v-else>Show Basic Mode   ⚆</p>
  </button>
  <div class="results">
   <input class="input" v-model="current" />
  </div>
  <div class="mode" v-if="changeMode">
   <button class="button" @click="press">7</button>
   <button class="button" @click="press">8</button>
   <button class="button" @click="press">9</button>
   <button class="button" @click="press">*</button>
   <button class="button" @click="press"><=</button>
   <button class="button" @click="press">C</button>
   <button class="button" @click="press">4</button>
   <button class="button" @click="press($event)">5</button>
   <button class="button" @click="press">6</button>
   <button class="button" @click="press">/</button>
   <button class="button" @click="press">(</button>
   <button class="button" @click="press">)</button>
   <button class="button" @click="press">1</button>
   <button class="button" @click="press">2</button>
   <button class="button" @click="press">3</button>
   <button class="button" @click="press">-</button>
   <button class="button" @click="press">x 2</button>
   <button class="button" @click="press">±</button>
   <button class="button" @click="press">0</button>
   <button class="button" @click="press">.</button>
   <button class="button" @click="press">%</button>
   <button class="button" @click="press">+</button>
   <button class="button equal-sign" @click="press">=</button> 
  </div>
  <div class="mode" v-else>
   <button class="button" @click="press">sin</button>
   <button class="button" @click="press">cos</button>
   <button class="button" @click="press">tan</button>
   <button class="button" @click="press">x^</button>
   <button class="button" @click="press"><=</button>
   <button class="button" @click="press">C</button>
   <button class="button" @click="press">log</button>
   <button class="button" @click="press">ln</button>
   <button class="button" @click="press">e</button>
   <button class="button" @click="press">°</button>
   <button class="button" @click="press">rad</button>
   <button class="button" @click="press">√</button>
   <button class="button" @click="press">7</button>
   <button class="button" @click="press">8  </button>
   <button class="button" @click="press">9</button>
   <button class="button" @click="press">/</button>
   <button class="button" @click="press">x 2</button>
   <button class="button" @click="press">x !</button>
   <button class="button" @click="press">4</button>
   <button class="button" @click="press">5</button>
   <button class="button" @click="press">6</button>
   <button class="button" @click="press">*</button>
   <button class="button" @click="press">(</button>
   <button class="button" @click="press">)</button>
   <button class="button" @click="press">1</button>
   <button class="button" @click="press">2</button>
   <button class="button" @click="press">3</button>
   <button class="button" @click="press">-</button>
   <button class="button" @click="press">%</button>
   <button class="button" @click="press">±</button>
   <button class="button" @click="press">0</button>
   <button class="button" @click="press">.</button>
   <button class="button" @click="press">π</button>
   <button class="button" @click="press">+</button>          
   <button class="button equal-sign" @click="press">=</button>
  </div>
 </div>
</div>

2. css部分:

body {
 background: linear-gradient(to right, #85D8CE, #085078);
}
#app {
 font-family: 'Avenir', Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 display: flex;
 flex-wrap: wrap;
 justify-content: center;
 align-item: center;
}
.calculator {
 width: 440px;
 padding: 20px;
 border-radius: 5px;
 margin: 20px auto;
 font-size: 16px;
 background-color: #333333;
}
.input {
 width: 420px;
 height: 50px;
 border-radius: 0px;
 border: 1px solid black;
 background-color: #333333;
 color: #d9d9d9;
 padding: 0 5px 0 5px;
 margin: 0 0px 10px 0px;
 font-size: 30px;
}
.input:focus,
.input:active {
 border-color: #03a9f4;
 box-shadow: 0 0 4px #03A9F4;
 outline: none 0;
}
.button {
 margin: 3px;
 width: 63px;
 border: 1px solid #0d0d0d;
 height: 30px;
 border-radius: 4px;
 color: #d9d9d9;
 background-color: #1a1a1a;
 cursor: pointer;
 outline: none;
}
.mode {
 display: flex;
 flex-wrap: wrap;
 justify-content: space-evenly;
}
.equal-sign {
 background-color: green;
 width: 133px;
}
.toggle-button {
 border: none;
 background-color: #333333;
 cursor: pointer;
 outline: none;
 font-size: 1rem;
 color: #fff;
 text-shadow: -1px -1px 0 rgba(0, 0, 0, 0.35);
}
p {
 margin-top: 0;
}
button::-moz-focus-inner {
 border-color: transparent;
}

3. js部分:

let app = new Vue({
 el: '#app',
 data () {
  return{ 
   current: '',
   changeMode: true
  }
 },
 methods: {
  press: function (event) {
   let me = this
   let key = event.target.textContent
   if (
    key != '=' && 
    key != 'C' &&
    key != '*' &&
    key != '/' &&
    key != '√' &&
    key != "x 2" &&
    key != "%" &&
    key != "<=" && 
    key != "±" && 
    key != "sin" && 
    key != "cos" && 
    key != "tan" && 
    key != "log" && 
    key != "ln" && 
    key != "x^" && 
    key != "x !" && 
    key != "π" && 
    key != "e" && 
    key != "rad" && 
    key != "°"
   ) {
    me.current += key
   } else if (key === '=') {
    if ((me.current).indexOf('^') > -1) {
     let base = (me.current).slice(0, (me.current).indexOf('^'))
     let exponent = (me.current).slice((me.current).indexOf('^') + 1)
     me.current = eval('Math.pow(' + base + ',' + exponent + ')')
    } else {
     me.current = eval(me.current)
    }
   } else if (key === 'C') {
    me.current = ''
   } else if (key === '*') {
    me.current += '*'
   } else if (key === '/') {
    me.current += '/'
   } else if (key === '+') {
    me.current += '+'
   } else if (key === '-') {
    me.current += '-'
   } else if (key === '±') {
    if ((me.current).charAt(0) === '-') {
     me.current = (me.current).slice(1)
    } else {
     me.current = '-' + me.current
    }
   } else if (key === '<=') {
    me.current = me.current.substring(0, me.current.length - 1)
   } else if (key === '%') {
    me.current = me.current / 100
   } else if (key === 'π') {
    me.current = me.current * Math.PI
   } else if (key === 'x 2') {
    me.current = eval(me.current * me.current)
   } else if (key === '√') {
    me.current = Math.sqrt(me.current)
   } else if (key === 'sin') {
    me.current = Math.sin(me.current)
   } else if (key === 'cos') {
    me.current = Math.cos(me.current)
   } else if (key === 'tan') {
    me.current = Math.tan(me.current)
   } else if (key === 'log') {
    me.current = Math.log10(me.current)
   } else if (key === 'ln') {
    me.current = Math.log(me.current)
   } else if (key === 'x^') {
    me.current += '^'
   } else if (key === 'x !') {
    let number = 1
    if (me.current === 0) {
     me.current = '1'
    } else if (me.current < 0) {
     me.current = NaN
    } else {
     let number = 1
     for (let i = me.current; i > 0; i--) {
      number *= i
     }
     me.current = number
    }
   } else if (key === 'e') {
    me.current = Math.exp(me.current)
   } else if (key === 'rad') {
    me.current = me.current * (Math.PI / 180)
   } else if (key === '°') {
    me.current = me.current * (180 / Math.PI)
   }
  },
  changeModeEvent: function() {
   let me = this
   me.changeMode = !me.changeMode
  }
 }
})

完整实例代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>3water.com vue.js计算器</title>
<style>
body {
 background: linear-gradient(to right, #85D8CE, #085078);
}
#app {
 font-family: 'Avenir', Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 display: flex;
 flex-wrap: wrap;
 justify-content: center;
 align-item: center;
}
.calculator {
 width: 440px;
 padding: 20px;
 border-radius: 5px;
 margin: 20px auto;
 font-size: 16px;
 background-color: #333333;
}
.input {
 width: 420px;
 height: 50px;
 border-radius: 0px;
 border: 1px solid black;
 background-color: #333333;
 color: #d9d9d9;
 padding: 0 5px 0 5px;
 margin: 0 0px 10px 0px;
 font-size: 30px;
}
.input:focus,
.input:active {
 border-color: #03a9f4;
 box-shadow: 0 0 4px #03A9F4;
 outline: none 0;
}
.button {
 margin: 3px;
 width: 63px;
 border: 1px solid #0d0d0d;
 height: 30px;
 border-radius: 4px;
 color: #d9d9d9;
 background-color: #1a1a1a;
 cursor: pointer;
 outline: none;
}
.mode {
 display: flex;
 flex-wrap: wrap;
 justify-content: space-evenly;
}
.equal-sign {
 background-color: green;
 width: 133px;
}
.toggle-button {
 border: none;
 background-color: #333333;
 cursor: pointer;
 outline: none;
 font-size: 1rem;
 color: #fff;
 text-shadow: -1px -1px 0 rgba(0, 0, 0, 0.35);
}
p {
 margin-top: 0;
}
button::-moz-focus-inner {
 border-color: transparent;
}
</style>
</head>
<body>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<div id="app">
 <div class="calculator">
  <button @click="changeModeEvent" class="toggle-button">
   <p v-if="changeMode">Show Advanced Mode   ⚈</p>
   <p v-else>Show Basic Mode   ⚆</p>
  </button>
  <div class="results">
   <input class="input" v-model="current" />
  </div>
  <div class="mode" v-if="changeMode">
   <button class="button" @click="press">7</button>
   <button class="button" @click="press">8</button>
   <button class="button" @click="press">9</button>
   <button class="button" @click="press">*</button>
   <button class="button" @click="press"><=</button>
   <button class="button" @click="press">C</button>
   <button class="button" @click="press">4</button>
   <button class="button" @click="press($event)">5</button>
   <button class="button" @click="press">6</button>
   <button class="button" @click="press">/</button>
   <button class="button" @click="press">(</button>
   <button class="button" @click="press">)</button>
   <button class="button" @click="press">1</button>
   <button class="button" @click="press">2</button>
   <button class="button" @click="press">3</button>
   <button class="button" @click="press">-</button>
   <button class="button" @click="press">x 2</button>
   <button class="button" @click="press">±</button>
   <button class="button" @click="press">0</button>
   <button class="button" @click="press">.</button>
   <button class="button" @click="press">%</button>
   <button class="button" @click="press">+</button>
   <button class="button equal-sign" @click="press">=</button> 
  </div>
  <div class="mode" v-else>
   <button class="button" @click="press">sin</button>
   <button class="button" @click="press">cos</button>
   <button class="button" @click="press">tan</button>
   <button class="button" @click="press">x^</button>
   <button class="button" @click="press"><=</button>
   <button class="button" @click="press">C</button>
   <button class="button" @click="press">log</button>
   <button class="button" @click="press">ln</button>
   <button class="button" @click="press">e</button>
   <button class="button" @click="press">°</button>
   <button class="button" @click="press">rad</button>
   <button class="button" @click="press">√</button>
   <button class="button" @click="press">7</button>
   <button class="button" @click="press">8  </button>
   <button class="button" @click="press">9</button>
   <button class="button" @click="press">/</button>
   <button class="button" @click="press">x 2</button>
   <button class="button" @click="press">x !</button>
   <button class="button" @click="press">4</button>
   <button class="button" @click="press">5</button>
   <button class="button" @click="press">6</button>
   <button class="button" @click="press">*</button>
   <button class="button" @click="press">(</button>
   <button class="button" @click="press">)</button>
   <button class="button" @click="press">1</button>
   <button class="button" @click="press">2</button>
   <button class="button" @click="press">3</button>
   <button class="button" @click="press">-</button>
   <button class="button" @click="press">%</button>
   <button class="button" @click="press">±</button>
   <button class="button" @click="press">0</button>
   <button class="button" @click="press">.</button>
   <button class="button" @click="press">π</button>
   <button class="button" @click="press">+</button>          
   <button class="button equal-sign" @click="press">=</button>
  </div>
 </div>
</div>
<script>
let app = new Vue({
 el: '#app',
 data () {
  return{ 
   current: '',
   changeMode: true
  }
 },
 methods: {
  press: function (event) {
   let me = this
   let key = event.target.textContent
   if (
    key != '=' && 
    key != 'C' &&
    key != '*' &&
    key != '/' &&
    key != '√' &&
    key != "x 2" &&
    key != "%" &&
    key != "<=" && 
    key != "±" && 
    key != "sin" && 
    key != "cos" && 
    key != "tan" && 
    key != "log" && 
    key != "ln" && 
    key != "x^" && 
    key != "x !" && 
    key != "π" && 
    key != "e" && 
    key != "rad" && 
    key != "°"
   ) {
    me.current += key
   } else if (key === '=') {
    if ((me.current).indexOf('^') > -1) {
     let base = (me.current).slice(0, (me.current).indexOf('^'))
     let exponent = (me.current).slice((me.current).indexOf('^') + 1)
     me.current = eval('Math.pow(' + base + ',' + exponent + ')')
    } else {
     me.current = eval(me.current)
    }
   } else if (key === 'C') {
    me.current = ''
   } else if (key === '*') {
    me.current += '*'
   } else if (key === '/') {
    me.current += '/'
   } else if (key === '+') {
    me.current += '+'
   } else if (key === '-') {
    me.current += '-'
   } else if (key === '±') {
    if ((me.current).charAt(0) === '-') {
     me.current = (me.current).slice(1)
    } else {
     me.current = '-' + me.current
    }
   } else if (key === '<=') {
    me.current = me.current.substring(0, me.current.length - 1)
   } else if (key === '%') {
    me.current = me.current / 100
   } else if (key === 'π') {
    me.current = me.current * Math.PI
   } else if (key === 'x 2') {
    me.current = eval(me.current * me.current)
   } else if (key === '√') {
    me.current = Math.sqrt(me.current)
   } else if (key === 'sin') {
    me.current = Math.sin(me.current)
   } else if (key === 'cos') {
    me.current = Math.cos(me.current)
   } else if (key === 'tan') {
    me.current = Math.tan(me.current)
   } else if (key === 'log') {
    me.current = Math.log10(me.current)
   } else if (key === 'ln') {
    me.current = Math.log(me.current)
   } else if (key === 'x^') {
    me.current += '^'
   } else if (key === 'x !') {
    let number = 1
    if (me.current === 0) {
     me.current = '1'
    } else if (me.current < 0) {
     me.current = NaN
    } else {
     let number = 1
     for (let i = me.current; i > 0; i--) {
      number *= i
     }
     me.current = number
    }
   } else if (key === 'e') {
    me.current = Math.exp(me.current)
   } else if (key === 'rad') {
    me.current = me.current * (Math.PI / 180)
   } else if (key === '°') {
    me.current = me.current * (180 / Math.PI)
   }
  },
  changeModeEvent: function() {
   let me = this
   me.changeMode = !me.changeMode
  }
 }
})
</script>
</body>
</html>

使用本站HTML/CSS/JS在线运行测试工具:http://tools.3water.com/code/HtmlJsRun,可得到如下测试运行效果:

vue.js实现的经典计算器/科学计算器功能示例

Javascript 相关文章推荐
javascript 自动转到命名锚记
Jan 10 Javascript
JavaScript Event学习第三章 早期的事件处理程序
Feb 07 Javascript
利用jq让你的div居中的好方法分享
Nov 21 Javascript
jQuery制作的别致导航有阴影背景高亮模式窗口
Apr 15 Javascript
jQuery获取标签文本内容和html内容的方法
Mar 27 Javascript
javascript运动框架用法实例分析(实现放大与缩小效果)
Jan 08 Javascript
详解XMLHttpRequest(二)响应属性、二进制数据、监测上传下载进度
Sep 14 Javascript
jQuery Pagination分页插件使用方法详解
Feb 28 Javascript
vue中的非父子间的通讯问题简单的实例代码
Jul 19 Javascript
解决vue-router中的query动态传参问题
Mar 20 Javascript
Node.js文件编码格式的转换的方法
Apr 27 Javascript
Vue extend的基本用法(实例详解)
Dec 09 Javascript
Vue.js实现的计算器功能完整示例
Jul 11 #Javascript
JavaScript类的继承方法小结【组合继承分析】
Jul 11 #Javascript
React中嵌套组件与被嵌套组件的通信过程
Jul 11 #Javascript
JSON数据中存在单个转义字符“\”的处理方法
Jul 11 #Javascript
JS实现动态生成html table表格的方法分析
Jul 11 #Javascript
vue监听键盘事件的快捷方法【推荐】
Jul 11 #Javascript
vue移动端实现红包雨效果
Jun 23 #Javascript
You might like
PHP中CURL的CURLOPT_POSTFIELDS参数使用细节
2014/03/17 PHP
PHP下载生成的csv文件及问题总结
2015/08/06 PHP
php获取远程文件的内容和大小
2015/11/03 PHP
详解php比较操作符的安全问题
2015/12/03 PHP
php生成curl命令行的方法
2015/12/14 PHP
从零开始学习jQuery (四) jQuery中操作元素的属性与样式
2011/02/23 Javascript
jQuery对象和Javascript对象之间转换的实例代码
2013/03/20 Javascript
基于javascript 闭包基础分享
2013/07/10 Javascript
Js+Jq获取URL参数的集中方法示例代码
2014/05/20 Javascript
轻量级javascript 框架Backbone使用指南
2015/07/24 Javascript
有关json_decode乱码及NULL的问题
2015/10/13 Javascript
深入理解jQuery.data() 的实现方式
2016/11/30 Javascript
Vue响应式添加、修改数组和对象的值
2017/03/20 Javascript
Vue.js2.0中的变化小结
2017/10/24 Javascript
使用layer弹窗和layui表单实现新增功能
2018/08/09 Javascript
python通过BF算法实现关键词匹配的方法
2015/03/13 Python
深入解读Python解析XML的几种方式
2016/02/16 Python
Python openpyxl 遍历所有sheet 查找特定字符串的方法
2018/12/10 Python
Python使用统计函数绘制简单图形实例代码
2019/05/15 Python
python调用matplotlib模块绘制柱状图
2019/10/18 Python
Django生成PDF文档显示网页上以及PDF中文显示乱码的解决方法
2019/12/17 Python
Python结合Window计划任务监测邮件的示例代码
2020/08/05 Python
python与idea的集成的实现
2020/11/20 Python
python help函数实例用法
2020/12/06 Python
使用CSS3的font-face字体嵌入样式的方法讲解
2016/05/13 HTML / CSS
AVIS安飞士奥地利租车官网:提供奥地利、欧洲和全世界汽车租赁
2016/11/29 全球购物
Street One瑞士:德国现代时装公司
2019/10/09 全球购物
公司授权委托书范文
2014/08/02 职场文书
领导干部群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
毕业横幅标语
2014/10/08 职场文书
2014小学语文教学工作总结
2014/12/17 职场文书
信仰纪录片观后感
2015/06/08 职场文书
悬崖上的金鱼姬观后感
2015/06/15 职场文书
婚育证明样本
2015/06/16 职场文书
Mysql中调试存储过程最简单的方法
2021/06/30 MySQL
mysql查找连续出现n次以上的数字
2022/05/11 MySQL