Vue实现计算器计算效果


Posted in Javascript onAugust 17, 2020

本文实例为大家分享了Vue实现计算器计算效果的具体代码,供大家参考,具体内容如下

Vue实现计算器计算效果

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width,initial-scale=1.0">
 <script src="../js/vue.js"></script>
 <title>compare</title>
 <style type="text/css">
 *{
 padding: 0;
 margin: 0;
 box-sizing: border-box;
 }
 body{
 background-color: #000000;
 }
 .panle{
 border: 1px solid #5f5f5f;
 width: 100vw;
 height: 29vh;
 font-size: 3.8125rem;
 color: #FFFFFF;
 text-align: right;
 position: relative;
 }
 .curr{
 display: block;
 position: absolute;
 width: inherit;
 bottom: 0;
 font-size: 3.5rem;
 }
 .operation{
 display: block;
 position: absolute;
 width: inherit;
 bottom: 80px;
 font-size: 2.875rem;
 }
 .prev{
 font-size: 2.875rem;
 display: block;
 position: absolute;
 width: inherit;
 bottom: 8rem;
 }
 .keyboad{
 display: flex;
 flex-flow: row wrap;
 margin: 0 calc((8vw - 16px) / 2);
 }
 .key{
 display: inline-block;
 border: 1px solid #333;
 width: 23vw;
 height: 23vw;
 border-radius: 50%;
 text-align: center;
 font-size: 30px;
 line-height: 23vw;
 margin: 2px;
 background-color: #616161;
 color: #ffffff;
 cursor: pointer;
 outline: none;
 border: none;
 box-shadow: 0 9px #999;
 }
 .key:active {
 box-shadow: 0 5px #666;
 transform: translateY(4px);
 }
 .key:last-child{
 border-radius: 30%;
 flex-grow: 1;
 margin: 0;
 }
 .highlight{
 background-color: #e77919;
 }
 
 </style>
 </head>
 <body>
 <div id="app">
 <div class="panle">
 <span class="prev">{{prevNum}}</span>
 <span class="operation">{{operation}}</span>
 <span class="curr">{{currNum}}</span>
 </div>
 <div class="keyboad">
 <div class="key highlight" @click="clear">AC</div>
 <div class="key highlight" @click="back"><-</div>
 <div class="key highlight">#</div>
 <div class="key highlight" @click="except">/</div>
 <div class="key">7</div>
 <div class="key">8</div>
 <div class="key">9</div>
 <div class="key highlight" @click="ride">*</div>
 <div class="key">4</div>
 <div class="key">5</div>
 <div class="key">6</div>
 <div class="key highlight" @click="reduce">-</div>
 <div class="key">1</div>
 <div class="key">2</div>
 <div class="key">3</div>
 <div class="key highlight" @click="add">+</div>
 <div class="key">0</div>
 <div class="key">.</div>
 <div class="key highlight" @click="result">=</div>
 </div>
 </div>
 <script type="text/javascript">
 let vm = new Vue({
 el:"#app",
 data(){
 return{
 operation:'',
 prevNum:'',
 currNum:'',
 keyboard:[],
 arr:[],
 res:''
 }
 },
 mounted() {
 this.keyboard = document.querySelectorAll('div[class=key]');
 Array.from(this.keyboard, key => key.addEventListener('click',this.getVal))
 },
 methods:{
 getVal(e){
 this.currNum += e.target.innerHTML;
 this.arr.push(e.target.innerHTML);
 },
 clear(){
 this.prevNum = this.operation = this.currNum = '';
 },
 back(){
 this.arr.splice(-1,1)
 this.currNum = this.arr.join('')
 },
 add(){
 this.prevNum = this.currNum;
 this.currNum = '';
 this.operation = '+';
 },
 reduce(){
 this.prevNum = this.currNum;
 this.currNum = '';
 this.operation = '-';
 },
 ride(){
 this.prevNum = this.currNum;
 this.currNum = '';
 this.operation = '*';
 },
 except(){
 this.prevNum = this.currNum;
 this.currNum = '';
 this.operation = '/';
 },
 result(){
 switch(this.operation){
 case'+':
 this.res = Number(this.currNum) + Number(this.prevNum);
 break;
 case'-':
 this.res = Number(this.prevNum) - Number(this.currNum);
 break;
 case'*':
 this.res = Number(this.prevNum) * Number(this.currNum);
 break;
 case'/':
 this.res = Number(this.prevNum) / Number(this.currNum);
 break;
 }
 this.clear();
 this.currNum = this.res; 
 this.arr = [];
 }
 }
 })
 </script>
 </body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
点击广告后才能获得下载地址
Oct 26 Javascript
jQuery 前的按键判断代码
Mar 19 Javascript
深入理解javaScript中的事件驱动
May 21 Javascript
jQuery插件开发精品教程(让你的jQuery更上一个台阶)
Nov 07 Javascript
jquery $.trim()去除字符串空格的实现方法【附图例】
Mar 30 Javascript
jQuery中设置form表单中action值的实现方法
May 25 Javascript
使用vue.js制作分页组件
Jun 27 Javascript
jQuery模拟Marquee实现无缝滚动效果完整实例
Sep 29 Javascript
浅谈jQuery中Ajax事件beforesend及各参数含义
Dec 03 Javascript
vue params、query传参使用详解
Sep 12 Javascript
webpack4之SplitChunksPlugin使用指南
Jun 12 Javascript
layui(1.0.9)文件上传upload,前后端的实例代码
Sep 26 Javascript
vue-model实现简易计算器
Aug 17 #Javascript
Vue实现手机计算器
Aug 17 #Javascript
Vuex实现购物车小功能
Aug 17 #Javascript
jquery+ajax实现异步上传文件显示进度条
Aug 17 #jQuery
jQuery实现异步上传一个或多个文件
Aug 17 #jQuery
vue项目里面引用svg文件并给svg里面的元素赋值
Aug 17 #Javascript
vue动态加载SVG文件并修改节点数据的操作代码
Aug 17 #Javascript
You might like
三国漫画《火凤燎原》宣布动画化PV放出 预计2020年播出
2020/03/08 国漫
php图片上传存储源码并且可以预览
2011/08/26 PHP
Smarty的配置与高级缓存技术分享
2012/06/05 PHP
php读取3389的脚本
2014/05/06 PHP
新手入门常用代码集锦
2007/01/11 Javascript
JS 时间显示效果代码
2009/08/23 Javascript
JavaScript 学习初步 入门教程
2010/03/25 Javascript
jquery 模拟类搜索框自动完成搜索提示功能(改进)
2010/05/24 Javascript
JavaScript.Encode手动解码技巧
2010/07/14 Javascript
使用Firebug对js进行断点调试的图文方法
2011/04/02 Javascript
基于jQuery捕获超链接事件进行局部刷新代码
2012/05/10 Javascript
上传的js验证(图片/文件的扩展名)
2013/04/25 Javascript
深入分析下javascript中的[]()+!
2015/07/07 Javascript
简介alert()与console.log()的不同
2015/08/26 Javascript
js获取对象、数组的实际长度,元素实际个数的实现代码
2016/06/08 Javascript
jQuery中常用动画效果函数(日常整理)
2016/09/17 Javascript
JS判断浏览器类型与操作系统的方法分析
2020/04/30 Javascript
Python查询Mysql时返回字典结构的代码
2012/06/18 Python
Python基本数据类型详细介绍
2014/03/11 Python
Python使用metaclass实现Singleton模式的方法
2015/05/05 Python
对python 数据处理中的LabelEncoder 和 OneHotEncoder详解
2018/07/11 Python
python pandas读取csv后,获取列标签的方法
2018/11/12 Python
对python GUI实现完美进度条的示例详解
2018/12/13 Python
Python实现的大数据分析操作系统日志功能示例
2019/02/11 Python
python 求一个列表中所有元素的乘积实例
2019/06/11 Python
美国顶级户外凉鞋品牌:Chacos
2017/03/27 全球购物
美国杰西潘尼官网:JCPenney
2019/06/12 全球购物
美国家居装饰网上商店:Lulu & Georgia
2019/09/14 全球购物
俄罗斯便宜的在线服装商店:GroupPrice
2020/04/10 全球购物
新闻学毕业生自荐信
2013/11/15 职场文书
通信工程毕业生求职信
2013/11/16 职场文书
2016年清明节期间群众祭祀活动工作总结
2016/04/01 职场文书
小学2016年第十八届推普周活动总结
2016/04/05 职场文书
2016年主题党日活动总结
2016/04/05 职场文书
Python离线安装openpyxl模块的步骤
2021/03/30 Python
解析在浏览器地址栏输入一个URL后发生了什么
2021/06/21 Servers