Vue实现手机计算器
编程学习 2021-07-04 14:06www.dzhlxh.cn编程入门
这篇文章主要为大家详细介绍了Vue实现手机计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Vue制作仿手机计算器的具体代码,供大家参考,具体内容如下
1.首先是把样式做出来,按钮是0-9,还有加减乘除,百分号,清除按钮,小数点,等号、等等
2.把官方网站的JS插件引用,
页面视图
JS
new Vue({ el: "#app", data: { equation: '0', isDecimalAdded: false, //防止在一组数字中间输入超过一个小数位 isOperatorAdded: false, //判断之否已点击 加、减、乘、除,防止连续点击超过一个运算符号 isStarted: false, //判断计算器是否已经开始输入数字,用于正负数和百分比计算的时候作一些判断 }, 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() { //等于号的时候 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() { //点击正负号 if (this.isOperatorAdded || !this.isStarted) { true } this.equation = this.equation + '* -1' this.calculate() }, calculatePercentage() { //点击百分比 if (this.isOperatorAdded || !this.isStarted) { true } this.equation = this.equation + '* 0.01' this.calculate() }, clear() { //点击AC this.equation = '0' this.isDecimalAdded = false //防止在一组数字中间输入超过一个小数位 this.isOperatorAdded = false //判断之否已点击 加、减、乘、除,防止连续点击超过一个运算符号 this.isStarted = false //判断计算器是否已经开始输入数字,用于正负数和百分比计算的时候作一些判断 } } })
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
上一篇:vue-model实现简易计算器
下一篇:Vuex实现购物车小功能
编程语言
- ThinkPad 笔记本如何调节LCD屏幕亮度
- Dreamweaver制作网页打开特效教程
- Win10 Mobile 10586升级后无限重启怎么办 硬重启帮您
- Win8系统提示音频设备有问题有一个或多个音频服
- Xbox One版Win10首个预览版9月份发布
- 如何在textarea文本输入区内实现换行
- Win10 Build 9901系统更新 预览版新版本下载
- McAfee Framework存在远程格式串处理漏洞
- Win10家庭版今日(7月30)正式在中国官方商城开卖
- Win10 Mobile预览版更新完10536.1000后才收到10536.100
- Win10 RS2更新了什么-Win10 RS2最终版本号1704首曝
- Windows7如何查看回收站对应的文件夹有哪些方法
- Win10让Charms栏回归桌面的方法教程
- 取消Windows XP系统开机启动画面的小技巧
- win8系统怎么下载安装USB百兆网卡?
- XP系统下磁盘空间变少了怎么办?XP系统磁盘空间