西南IT社区
    • 注册
    • 登录
    • 搜索
    • 主页
    • 问答
    • 话题
    • 热门
    • 圈子
    • 工作机会
    • 活动
    • 项目

    使用 vue 创建一个多功能计算器

    开源推荐
    计算器
    1
    1
    103
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • S
      sena4654 最后由 编辑

      先来一张效果图

      5c6ca00f-2af9-4bb6-b82f-5c0c439002e1-image.png

      代码

      html

      <div id="app">
          <div class="calculator">
            <button @click="changeModeEvent" class="toggle-button">
              <p v-if="changeMode">切换高级模式 &nbsp; &nbsp; &#9864;</p>
              <p v-else>切换简单模式 &nbsp; &nbsp; &#9862;</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">&#60;=</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 ²</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">&#60;=</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 ²</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">&#x003C0;</button>
              <button class="button" @click="press">+</button>
              <button class="button equal-sign" @click="press">=</button>
            </div>
          </div>
        </div>
      
        <script src='js/vue.min.js'></script>
        <script src="js/index.js"></script>
      

      css

      @import "https://cdn.bootcdn.net/ajax/libs/normalize/8.0.1/normalize.min.css";
      
      body {
        background: linear-gradient(to right, #85D8CE, #085078);
      }
      
      #app {
        font-family: 'Microsoft Yahei', 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;
        margin-top: 80px;
      }
      
      .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;
      }
      

      js

      'use strict';
      
      var app = new Vue({
        el: '#app',
        data: function data() {
          return {
            current: '',
            changeMode: true
          };
        },
      
        methods: {
          press: function press(event) {
            var me = this;
            var key = event.target.textContent;
      
            if (key != '=' && key != 'C' && key != '*' && key != '/' && key != '√' && key != "x ²" && 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) {
                var base = me.current.slice(0, me.current.indexOf('^'));
                var 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 ²') {
      
              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 !') {
      
              var number = 1;
              if (me.current === 0) {
                me.current = '1';
              } else if (me.current < 0) {
                me.current = NaN;
              } else {
                var _number = 1;
                for (var 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 changeModeEvent() {
            var me = this;
            me.changeMode = !me.changeMode;
          }
        }
      });
      

      参考来源:

      https://www.jsdaima.com/Uploads/js/201803/1520473098/index.html

      1 条回复 最后回复 回复 引用 0
      • First post
        Last post
      使用HTML构建办公软件 使用HTML构建办公软件 使用HTML构建办公软件
      S
      成
      Y
      洋
      书
      Y
      D
      U
      Y
      娇
      玩
      1
      光
      A
      庆
      小
      U
      Y
      L
      I
      Z
      I
      Y
      C
      Z

      西南地区IT社群(QQ)
      云南
      【昆明网页设计交流吧】243627302
      【昆明nodejs交流吧】 243626749
      【VUE】838405306
      【云南程序员总群】343606807
      【昆明UI设计】104031254
      【云南软件外包】15547313
      贵州
      【PHP/java源码/站长交流群】55692114
      四川
      【成都Java/JavaWeb交流】86669225
      【vaScript+PHP+MySql】116270060
      【UI设计/设计交流学习群】135794928
      重庆
      【诺基亚 JAVA游戏博物馆】 559479780
      【PHP,Java,Python,C++接单】 442103442
      西藏
      社群
      昆明网页设计交流吧
      友情链接
      • Funtask
      • Funtask 社区
      • SUWIS
      ©2019-2021 滇ICP备20006698号