WeChat applet calculator example

WeChat applet calculator example

WeChat applet calculator example, for your reference, the specific content is as follows

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 .operator {
  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: "", // store numbers op: "" // store operators},
    result: null,  
    isClear: false,

    numBtn: function(e) {
        var num = e.target.dataset.val
            //console.log(num) gets the value of 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: "" })
    }
})

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • WeChat applet implements a simple calculator
  • Implementing calculator functions with WeChat applet
  • WeChat applet implements simple calculator function
  • WeChat applet implements calculator function
  • WeChat applet implements calculator function
  • WeChat applet implements simple calculator function
  • WeChat applet implements a simple calculator
  • WeChat applet simple calculator implementation code example
  • WeChat applet calculator example

<<:  MySQL establishes efficient index example analysis

>>:  Nginx dynamically forwards to upstream according to the path in the URL

Recommend

Design Tips: We think you will like it

<br />Looking at this title, you may find it...

The principle and direction of JavaScript this

How to determine what this points to? ①When calle...

JS+Canvas draws a lucky draw wheel

This article shares the specific code of JS+Canva...

HTML form tag usage learning tutorial

Forms in HTML can be used to collect various type...

Win10 + Ubuntu20.04 LTS dual system boot interface beautification

Effect display The built-in boot interface is too...

How to call the browser sharing function in Vue

Preface Vue (pronounced /vjuː/, similar to view) ...

Example of viewing and modifying MySQL transaction isolation level

Check the transaction isolation level In MySQL, y...

Start a local Kubernetes environment using kind and Docker

introduce Have you ever spent a whole day trying ...

Steps to use ORM to add data in MySQL

【Foreword】 If you want to use ORM to operate data...

Understanding flex-grow, flex-shrink, flex-basis and nine-grid layout

1. flex-grow, flex-shrink, flex-basis properties ...

2 reasons why html-css tag style setting does not work

1 CSS style without semicolon ";" 2 Tags...

In-depth understanding of the use of Vue

Table of contents Understand the core concept of ...

Analyze the usage and principles of Vue's provide and inject

First, let's talk about why we use provide/in...