本文实例讲述了vue.js实现的经典计算器/科学计算器功能。分享给大家供大家参考,具体如下:
1. HTML部分:
<script src="/UploadFiles/2021-04-02/vue.min.js">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>www.jb51.net 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="/UploadFiles/2021-04-02/vue.min.js">使用本站HTML/CSS/JS在线运行测试工具:http://tools.jb51.net/code/HtmlJsRun,可得到如下测试运行效果:
PS:这里再为大家推荐几款计算工具供大家参考:
在线数学表达式简单转换/计算工具:
http://tools.jb51.net/jisuanqi/exp_jisuanqi在线一元函数(方程)求解计算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi科学计算器在线使用_高级计算器在线计算:
http://tools.jb51.net/jisuanqi/jsqkexue在线计算器_标准计算器:
http://tools.jb51.net/jisuanqi/jsq希望本文所述对大家vue.js程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 张惠妹.2011-A.MEI.ACOUSTIC.BEST.2CD【丰华】【WAV+CUE】
- ZEN.1996-珍惜所有【华纳】【WAV+CUE】
- 群星《环球国语元素》香港首版[WAV+CUE][1G]
- 周慧敏《玉女天后》原音母版1:1直刻[WAV+CUE][1G]
- 谭咏麟《20世纪中华歌坛名人百集珍藏版》[WAV+CUE][1G]
- 炉石传说40轮盘术最新卡组代码在哪找 标准40轮盘术卡组代码分享
- 炉石传说亲王贼怎么玩 2024亲王贼最新卡组代码分享
- 炉石传说30.6.2补丁后有什么卡组 30.6.2最强卡组最新推荐
- 模拟之声慢刻CD《蔡琴名曲回顾遇听》[原抓WAV+CUE]
- BruceLiu-WAVES(MusicbySatie)(2024)2CD[24Bit-96kHz]FLAC
- KonstantinKrimmel-MythosSchubertLoewe(2024)[24Bit-96kHz]FLAC
- 2024雷蛇高校挑战赛 嘤式分解助力收官之战
- 海信发布110吋世俱杯官方定制AI电视 引领智能观赛
- 海信发布27英寸显示器大圣G5 Pro:采用自研超解析芯片、友达原厂模组
- 蔡琴《机遇》1:1母盘直刻日本头版[WAV分轨][1.1G]