资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

微信小程序怎么实现计算器-创新互联

本篇内容介绍了“微信小程序怎么实现计算器”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

网站的建设成都创新互联公司专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为水处理设备等企业提供专业服务。

效果如下:

微信小程序怎么实现计算器

index.wxml


    {{num}}
    {{op}}


    
        c
        DEL
        %
        ÷
    
    
        7
        8
        9
        x
    
    
        4
        5
        6
        -
    
    
        1
        2
        3
        +
    
    
        0
        .
        =
    

index.css

page {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.content {
  flex: 1;
  background-color: #f3f6fe;
  position: relative;
}
.content .num {
  position: absolute;
  font-size: 27pt;
  bottom: 5vh;
  right: 3vw;
}
.content .operotor {
  font-size: 15pt;
  position: absolute;
  bottom: 1vh;
  right: 3vw;
}
.entry {
  flex: 1;
  font-size: 17pt;
  border-top: 1rpx solid #ccc;
}
.entry .item {
  flex: 1;
  padding: 30rpx 0;
  text-align: center;
  flex-basis: 25%;
  border-left: 1rpx solid #ccc;
  border-bottom: 1rpx solid #ccc;
}
.entry > view {
  display: flex;
}
.entry > view .tow {
  flex: 2;
}
.entry > view .one {
  flex: 1;
}

index.js

Page({
    data: {
        num: "",  // 存储数字
        op: ""    //存储运算符
    },
    result: null,  
    isClear: false,

    numBtn: function(e) {
        var num = e.target.dataset.val
            //console.log(num) 得到data-val的值
        console.log(this.isClear)
        if (this.data.num === "0" || this.isClear) {
            this.setData({ num: num })
            this.isClear = false
        } else {
            this.setData({ num: this.data.num + num })
        }
    },

    opBtn: function(e) {
        var op = this.data.op
        var num = Number(this.data.num)
        this.setData({ op: e.target.dataset.val })
        if (this.isClear) {
            return
        }
        this.isClear = true
        if (this.result === null) {
            this.result = num
            return
        }
        if (op === "+") {
            this.result = this.result + num
        } else if (op === "-") {
            this.result = this.result - num
        } else if (op === "*") {
            this.result = this.result * num
        } else if (op === "/") {
            this.result = this.result / num
        } else if (op === "%") {
            this.result = this.result % num
        }
        this.setData({ num: this.result })
    },

    dotBtn: function() {
        if (this.isClear) {
            this.setData({ num: "0." })
            this.isClear = false
            return
        }
        if (this.data.num.indexOf(".") >= 0) {
            return
        }
        this.setData({ num: this.data.num + "." })
    },
    delBtn: function() {

        var num = this.data.num.substr(0, this.data.num.length - 1)
        this.setData({ num: num === "" ? "0" : num })
    },
    resetBtn: function() {
        this.result = null
        this.isClear = false
        this.setData({ num: "0", op: "" })
    }
})

“微信小程序怎么实现计算器”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


新闻名称:微信小程序怎么实现计算器-创新互联
标题路径:http://cdkjz.cn/article/dgjihc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220