Vue-简易计算器

1、通过v-model指令实现数值a和数值b的绑定

2、给计算按钮绑定事件,实现计算逻辑

3、将计算结果绑定到对应的位置

<!DOCTYPE html>
<html lang="en">

<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">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .item {
            cursor: pointer;
        }

        .item:hover {
            background: #ccc;
        }

        .container_box {
            display: flex;
            flex-wrap: wrap;
            width: 186px;
            height: 248px;
            float: left;
            box-sizing: border-box;
        }

        .container_box .item {
            width: 60px;
            height: 60px;
            font-size: 24px;
            text-align: center;
            border: 1px solid #000;
            line-height: 60px;
            color: #333;
        }

        .btns {
            width: 60px;
            height: 248px;
            float: left;
        }

        .btns .item {
            width: 60px;
            height: 60px;
            font-size: 24px;
            text-align: center;
            border: 1px solid #000;
            line-height: 60px;
            color: #333;
            width: 60px
        }

        .res {
            width: 116px;
            height: 30px;
            border: 1px solid #000;
            line-height: 30px;
            text-align: center;
            cursor: pointer;
            display: inline-block;
            margin-top: 5px;
        }

        .res:hover {
            background: #ddd
        }

        .sign {
            margin-top: 20px;
            font-size: 28px;
            font-weight: bold;
            padding: 15px;
            color: rgb(0, 119, 167);
        }

        .clearfix::after {
            content: '.';
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- 数值和运算符 -->
        <div class="opa clearfix">
            <!-- 数值 -->
            <div class="container_box" @click="getValue">
                <div class="item">7</div>
                <div class="item">8</div>
                <div class="item">9</div>
                <div class="item">4</div>
                <div class="item">5</div>
                <div class="item">6</div>
                <div class="item">1</div>
                <div class="item">2</div>
                <div class="item">3</div>
                <div class="item">0</div>
                <div class="item" style="width:122px">.</div>
            </div>
            <!-- 运算符 -->
            <div class="btns" @click="getSign">
                <div class="item">+</div>
                <div class="item">-</div>
                <div class="item">*</div>
                <div class="item">/</div>
            </div>
        </div>
        <div class="wrap">
            <div class="res" @click="getReset">重置</div>
            <div class="res" @click="getRel">计算</div>
        </div>
        <div class="sign">
            <span>{{num1}}</span>
            <span>{{sign}}</span>
            <span>{{num2}}</span>
            <span>{{equal}}</span>
            <span>{{rel}}</span>
        </div>
    </div>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                num1: '',
                sign: '',
                num2: '',
                equal: '',
                rel: '',
                flag: false
            }, methods: {
                getValue(e) {
                    if (!this.flag) {
                        this.num1 += e.target.innerText
                    }
                    if (this.flag) {
                        this.num2 += e.target.innerText
                    }
                },
                getSign(e) {
                    this.flag = true;
                    this.sign = e.target.innerText
                },
                getRel() {
                    this.equal = '=';
                    switch (this.sign) {
                        case '+':
                            this.rel = Number(this.num1) + Number(this.num2);
                            break;
                        case "-":
                            this.rel = this.num1 - this.num2;
                            break;
                        case "*":
                            this.rel = this.num1 * this.num2;
                            break;
                        case "/":
                            this.rel = this.num1 / this.num2;
                            break;
                        default:
                            this.rel = '空';
                            break;
                    }
                },
                getReset() {
                    this.num1 = '';
                    this.num2 = '';
                    this.sign = '';
                    this.rel = '';
                    this.equal = '';
                    this.flag = false;
                }
            }
        })
    </script>
</body>

</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值