vue实现简易计算器

实现效果

在这里插入图片描述

源代码

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>简易计算器</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        #app {
            margin: 50px auto;
            padding: 10px;
            width: 400px;
            height: 600px;
            background-color: cyan;
        }
        
        .res {
            margin-bottom: 10px;
            height: 150px;
        }
        
        .res .p1 {
            padding-left: 10px;
            height: 100px;
            line-height: 100px;
            font-size: 30px;
            background-color: darkorange;
        }
        
        .res .p2 {
            padding-right: 10px;
            height: 50px;
            line-height: 50px;
            background-color: greenyellow;
            text-align: right;
            font-size: 30px;
        }
        
        .key {
            height: 440px;
            background-color: darksalmon;
        }
        
        .key .a {
            float: left;
            margin: 20px 10px 40px 10px;
            width: 80px;
            height: 50px;
            font-size: 20px;
        }
        
        .key .b {
            float: left;
            margin: 0 10px 30px 10px;
            width: 113px;
            height: 50px;
            font-size: 20px;
        }
        
        .key .c {
            float: left;
            margin: 10px;
            width: 113px;
            height: 50px;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="res">
            <p class="p1">{{str}}</p>
            <p class="p2">{{res}}</p>
        </div>
        <div class="key">
            <button class="a" @click='press'>+</button>
            <button class="a" @click='press'>-</button>
            <button class="a" @click='press'>*</button>
            <button class="a" @click='press'>/</button>
            <button class="b" @click='press'>9</button>
            <button class="b" @click='press'>8</button>
            <button class="b" @click='press'>7</button>
            <button class="b" @click='press'>6</button>
            <button class="b" @click='press'>5</button>
            <button class="b" @click='press'>4</button>
            <button class="b" @click='press'>3</button>
            <button class="b" @click='press'>2</button>
            <button class="b" @click='press'>1</button>
            <button class="c" @click='press'>0</button>
            <button class="c" @click='press'>C</button>
            <button class="c" @click='press'>=</button>
        </div>
    </div>
    <script src="./vue.js"></script>
    <script>
        // 实例化一个vue对象
        var vm = new Vue({
            el: "#app",
            data: {
                str: '',
                res: ''
            },
            methods: {
                press(e) {
                    var key = event.target.textContent;
                    if (key != '+' && key != '=' && key != '-' && key != '*' && key != '/' && key != 'C') {
                        this.str += key;
                    } else if (key === 'C') {
                        this.str = '';
                        this.res = '';
                    } else if (key === '+') {
                        this.str += '+';
                    } else if (key === '-') {
                        this.str += '-';
                    } else if (key === '*') {
                        this.str += '*';
                    } else if (key === '/') {
                        this.str += '/';
                    } else if (key === '=') {
                        this.res = eval(this.str); //eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码
                    }
                }
            }
        })
    </script>
</body>

</html>
Logo

前往低代码交流专区

更多推荐