微信小程序实现简单计算器与秒表
作者:枫渝浪天下 发布时间:2024-04-18 09:31:23
标签:微信小程序,计算器,秒表
本文实例为大家分享了微信小程序实现简单计算器与秒表的具体代码,供大家参考,具体内容如下
实验内容:
任务一:实现一个简单的加减乘除运算。
首先输入两个运算数,然后选择加、减、乘、除四个运算中的某一个运算按钮(共4个按钮),最后在界面上显示运算结果。运算数及运算结果支持整数和浮点数。
任务二:设计一个计数秒表。
不要求绘制秒表表盘、表针,只要求以数字的方式显示秒表计数即可。注意:显示形式为:分钟:秒数:百分之一秒数。(如果不清楚可以看看自己手机上的秒表数字显示)。
界面上设计一个按钮,计数未开始时,按钮显示文字为“开始“,点击后开始计数,并且按钮的显示文字变成”停止“,如果再次点击按钮则计数停止。
实验效果:
实验代码目录:
countingWatch 目录中放的是 秒表代码, index目录中放的是 简单计算器代码
实验代码:
简单计算器代码:
index.js
// index.js
const app = getApp()
Page({
data: {
describe: "计算",
num1: null,
num2: null,
result: 0
},
input1(e) {
this.setData({
num1: parseFloat(e.detail.value)
})
},
input2(e) {
this.setData({
num2: parseFloat(e.detail.value)
})
},
addButton(e) {
if (this.data.num1 && this.data.num2) {
this.setData({
describe: "加法",
result: this.data.num1 + this.data.num2
})
}
},
subButton(e) {
if (this.data.num1 && this.data.num2) {
this.setData({
describe: "减法",
result: this.data.num1 - this.data.num2
})
}
},
mulButton(e) {
if (this.data.num1 && this.data.num2) {
this.setData({
describe: "乘法",
result: this.data.num1 * this.data.num2
})
}
},
divButton(e) {
if (this.data.num1 && this.data.num2) {
this.setData({
describe: "除法",
result: this.data.num1 / this.data.num2
})
}
},
jump:function(){
wx.navigateTo({
url: '../countingWatch/countingWatch'
})
}
})
index.wxml
<!--index.wxml-->
<view class="firstNum">
<!-- <text>请输入第一个运算数:</text> -->
<label class="text" >请输入第一个运算数: </label>
<input type="digit" bindinput="input1" style=" border: 2rpx solid #ccc; width:150px; margin-left: 5px; "/>
</view>
<view class="secondNum">
<text class="text">请输入第二个运算数:</text>
<input type="digit" bindinput="input2" style=" border: 2rpx solid #ccc; width:150px; margin-left: 5px;"/>
</view>
<view class="describe">
<button bindtap="addButton" style="width: 30rpx;">+</button>
<button bindtap="subButton" style="width: 30rpx">
-</button>
<button bindtap="mulButton" style="width: 30rpx" >
*</button>
<button bindtap="divButton" style="width: 30rpx">
/</button>
</view>
<view class="result">
<text>{{describe}}结果:{{result}}</text>
</view>
<button bindtap="jump" class="jump">跳转至秒表</button>
index.wxss
/**index.wxss**/
.text{
font-size: 1.5ex;
font-weight: 600;
}
.firstNum,
.secondNum,
.result {
margin: 50rpx;
display: flex;
flex-direction: row;
height:50px;
}
.describe {
display: flex;
justify-content: space-evenly;
}
.describe button {
display: flex;
align-items: center;
justify-content: center;
color: black;
background-color: aqua;
}
.jump{
background: rgb(204, 19, 221);
margin-top: 100px;
}
秒表代码:
countingWatch.js
// pages/countingWatch/countingWatch.js
const app = getApp()
Page({
data: {
timer:null,
minute: 0, // 分
second: 0 , // 秒
millisecond:0,
describe:'开始',
timeformat:'00:00:00'
},
//计时开始
start: function () {
if(this.data.describe == "开始"){
this.setData({
describe:"停止"
})
this.setData({
minute:0,
second:0,
millisecond:0
})
this.data.timer = setInterval(this.counter,50)
}else{
this.setData({
describe:"开始"})
//这个是系统提供的用于时钟暂停的方法
clearInterval(this.data.timer)
}
},
counter:function(){
var second = this.data.second
var minute = this.data.minute
var millisecond = this.data.millisecond
this.setData({
millisecond:millisecond+5
})
if(millisecond >=99){
this.setData({
millisecond:0,
second:second+1
})
}
if(second == 60){
this.setData({
second : 0,
minute:minute+1
})
}
this.setData({
timeformat:minute+":"+second+":"+millisecond
})
},
jump:function(){
wx.navigateTo({
url: '../index/index'
})
}
})
countingWatch.wxml
<!--pages/countingWatch/countingWatch.wxml-->
<view class="timeformat">{{timeformat}}</view>
<button bindtap="start">{{describe}}</button>
<button class="jump" bindtap="jump">跳转至计算器</button>
countingWatch.wxss
/* pages/countingWatch/countingWatch.wxss */
button{
width:150rpx;
background: rgb(51, 231, 15);
color: #fff;
margin-bottom: 8px;
}
.timeformat{
margin: 20px;
text-align: center;
font-weight: 600;
font-size: 30px;
}
.jump{
background: rgb(204, 19, 221);
margin-top: 100px;
}
还有一个用于衔接两个页面的代码
app.json
{
"pages": [
"pages/index/index",
"pages/countingWatch/countingWatch",
"pages/logs/logs"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "两个数的运算",
"navigationBarTextStyle": "black"
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
来源:https://blog.csdn.net/qq_51901495/article/details/124576999


猜你喜欢
- 自己从工艺品设计到平面设计到网络设计,虽然设计原则不离其宗,但经验下来的心得告诉自己,设计媒介的变化带来很多媒介自身的特殊性,下面总结下网站
- 表单类控件承载了一个网页数据的录入与交互,本章将介绍如何使用指令v-model完成表单的数据双向绑定。6.1 基本用法表单控件在实际业务较为
- 本文实例为大家分享了Python爬取最好大学网大学排名的具体代码,供大家参考,具体内容如下源代码:#-*-coding:utf-8-*- &
- 前言:上一篇博客我用AOP+AbstractRoutingDataSource实现了MySQL读写分离,自己写代码实现判断该使用哪个数据源挺
- Vue加载流程1.初始化的第一阶段是Vue实例也就是vm对象创建前后:首先Vue进行生命周期,事件初始化发生在beforeCreate生命周
- Django是个好工具,使用的很广泛。 在应用比较小的时候,会觉得它很快,但是随着应用复杂和壮大,就显得没那么高效了。当你了解所用的Web框
- 一、背景先要从 InnoDB 的索引实现说起,InnoDB 有两大类索引:聚集索引 (clustered index)普通索引 (secon
- 先看map。map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回。举
- 游戏介绍在游戏中,魔术师要每位观众心里想一个三位数abc (a、b、c分别是百位、十位和个位数字),然后魔术师让观众心中记下acb、bac、
- 本文实例讲述了python计算方程式根的方法。分享给大家供大家参考。具体实现方法如下:''' roots = pol
- 富文本编辑器,Rich Text Editor, 简称 RTE, 它提供类似于 Microsoft Word 的编辑功能,容易被不会编写 H
- Pyqt5安装并配置到pycharm方法:教你如何用pycharm安装pyqt5及其相关配置一、简介QLabel是界面中的标签类,继承自QF
- 在使用go mod的过程中,发现不容易指定版本号尤其是没有打tag的,不知道怎么指定版本号, 不知道有哪版本号正常使用都没有问题,但是当引用
- Django项目要操作数据库,首先要和数据库建立连接,才能让程序中的数据和数据库关联起来进行数据的增删改查操作Django项目默认使用mys
- 要处理文本数据,需要比数字类型的数据更多的清理步骤。为了从文本数据中提取有用和信息,通常需要执行几个预处理和过滤步骤。Pandas 库有许多
- 本文实例为大家分享了python实现微信防撤回神器的具体代码,供大家参考,具体内容如下手写辛苦,希望给赞#!/usr/local/bin/p
- Java与mongodb的连接1. 连单台mongodbMongo mg = new Mongo();//默认连本机127.0.0.1 端口
- 如下所示:import osdef anyTrue(predicate, sequence):return True in map(pred
- 为什么要用numpy Python中提供了list容器,可以当作数组使用。但列表中的元素可以是任何对象,
- 今天给大家分享腾讯云的实名认证接口的调用点击免费获取产品from __future__ import print_functionimpor