欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序计算器实例详解

程序员文章站 2022-06-23 10:36:34
微信小程序计算器实例,供大家参考,具体内容如下index.wxmlindex.cssindex.js以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。...

微信小程序计算器实例,供大家参考,具体内容如下

微信小程序计算器实例详解

index.wxml

<view class="content">
    <view class="num">{{num}}</view>
    <view class="operotor">{{op}}</view>
</view>
<view class="entry">
    <view>
        <view class="item" bindtap="resetbtn">c</view>
        <view class="item" bindtap="delbtn">del</view>
        <view class="item" bindtap="opbtn" data-val="%">%</view>
        <view class="item" bindtap="opbtn" data-val="/">÷</view>
    </view>
    <view>
        <view class="item" bindtap="numbtn" data-val="7">7</view>
        <view class="item" bindtap="numbtn" data-val="8">8</view>
        <view class="item" bindtap="numbtn" data-val="9">9</view>
        <view class="item" bindtap="opbtn" data-val="*">x</view>
    </view>
    <view>
        <view class="item" bindtap="numbtn" data-val="4">4</view>
        <view class="item" bindtap="numbtn" data-val="5">5</view>
        <view class="item" bindtap="numbtn" data-val="6">6</view>
        <view class="item" bindtap="opbtn" data-val="-">-</view>
    </view>
    <view>
        <view class="item" bindtap="numbtn" data-val="1">1</view>
        <view class="item" bindtap="numbtn" data-val="2">2</view>
        <view class="item" bindtap="numbtn" data-val="3">3</view>
        <view class="item" bindtap="opbtn" data-val="+">+</view>
    </view>
    <view>
        <view class="item tow" bindtap="numbtn" data-val="0">0</view>
        <view class="item one" bindtap="dotbtn" data-val=".">.</view>
        <view class="item one" bindtap="opbtn" data-val="=">=</view>
    </view>
</view>

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: "" })
    }
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。