微信小程序实现计算器(含历史记录)
作者:灵魂学者 时间:2024-04-17 10:30:20
本文实例为大家分享了微信小程序实现计算器的具体代码,供大家参考,具体内容如下
1、实现效果图
2、代码编写
index.wxml
<!--author:LHXZ 灵魂学者-->
<!--pages/index/index.wxml-->
<view class="result">
<view class="historyed">|历史记录</view>
<scroll-view scroll-y bindscrolltolower class="historying">
<view wx:for="{{logs}}" wx:key="item" class="h_text">{{item}}</view>
</scroll-view>
<view class="clear" bindtap="clear">清空记录|</view>
<view class="history">{{history}}</view>
<view class="result-num">{{num}}</view>
<view class="result-op">{{op}}</view>
</view>
<view class="btns">
<view>
<view hover-class="bg" bindtap="resetBtn">C</view>
<view hover-class="bg" bindtap="delBtn">DEL</view>
<view hover-class="bg" bindtap="opBtn" data-val="%">%</view>
<view hover-class="bg" bindtap="opBtn" data-val="/">÷</view>
</view>
<view>
<view hover-class="bg" bindtap="numBtn" data-val="7">7</view>
<view hover-class="bg" bindtap="numBtn" data-val="8">8</view>
<view hover-class="bg" bindtap="numBtn" data-val="9">9</view>
<view hover-class="bg" bindtap="opBtn" data-val="*">×</view>
</view>
<view>
<view hover-class="bg" bindtap="numBtn" data-val="4">4</view>
<view hover-class="bg" bindtap="numBtn" data-val="5">5</view>
<view hover-class="bg" bindtap="numBtn" data-val="6">6</view>
<view hover-class="bg" bindtap="opBtn" data-val="-">-</view>
</view>
<view>
<view hover-class="bg" bindtap="numBtn" data-val="1">1</view>
<view hover-class="bg" bindtap="numBtn" data-val="2">2</view>
<view hover-class="bg" bindtap="numBtn" data-val="3">3</view>
<view hover-class="bg" bindtap="opBtn" data-val="+">+</view>
</view>
<view>
<view hover-class="bg" bindtap="numBtn" data-val="0">0</view>
<view hover-class="bg" bindtap="dotBtn">.</view>
<view hover-class="bg" bindtap="opBtn" data-val="=">=</view>
</view>
</view>
index.wxss
/* pages/index/index.wxss author:LHXZ 灵魂学者*/
page {
display: flex;
flex-direction: column;
height: 100%;
color: #555;
}
.result {
flex: 1;
background: #f3f6fe;
position: relative;
}
.result-num {
position: absolute;
font-size: 27pt;
bottom: 5vh;
right: 3vw;
}
.result-op {
font-size: 15pt;
position: absolute;
bottom: 1vh;
right: 3vw;
}
.btns {
flex: 1;
}
/* 按钮样式 */
.bg {
background: #eee;
}
.btns {
flex: 1;
display: flex;
flex-direction: column;
font-size: 17pt;
border-top: 1rpx solid #ccc;
border-left: 1rpx solid #ccc;
}
.btns > view {
flex: 1;
display: flex;
}
.btns > view > view {
flex-basis: 25%;
border-right: 1rpx solid #ccc;
border-bottom: 1rpx solid #ccc;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
}
.btns > view:last-child > view:first-child {
flex-basis: 50%;
}
.btns > view:first-child > view:first-child {
color: #f00;
}
.btns > view > view:last-child {
color: #fc8e00;
}
.history{
position: absolute;
top:20rpx;
right:40rpx;
font-size: 2rem;
color: rgb(199, 199, 199);
}
.h_text{
color: rgb(136, 136, 136);
margin-left: 20rpx;
}
.historying{
position: absolute;
top:200rpx;
}
scroll-view{
height: 200rpx;
background-color: rgba(255, 255, 255, 0.5);
}
.historyed{
position: absolute;
top:150rpx;
left: 10rpx;
font-size: 30rpx;
color: gray;
}
.clear{
position: absolute;
top:150rpx;
right: 10rpx;
font-size: 30rpx;
color: blue;
}
index.json
{
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "计算器",
"navigationBarTextStyle": "black",
"usingComponents": {
}
}
index.js
/*author:LHXZ 灵魂学者*/
Page({
/**
* 页面的初始数据
*/
data: {
num: '0',
op: '',
history:'',
logs:[]
},
result: null,
isClear: false,
// 数字按钮事件处理函数
numBtn: function(e) {
var num = e.target.dataset.val
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);
var newOp = e.target.dataset.val
this.setData({
op:newOp
})
if (this.isClear) {
this.setData({
history:this.data.history.substr(0,this.data.history.length-1)+newOp
})
return
}
this.setData({history:this.data.history+''+num+newOp})
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
}else if (op === '=') {
this.result =num
this.setData({
history:num + newOp
})
}
this.setData({
num: this.result + '',
})
// 历史记录
this.data.logs.unshift(this.data.history);
this.setData({
logs:this.data.logs
})
},
// 清空记录
clear:function(){
this.setData({
logs:[]
})
},
// 小数点事件处理函数
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 + '.'
})
},
// DEL按钮处理函数
delBtn: function() {
var num = this.data.num.substr(0, this.data.num.length - 1)
this.setData({
num: num === '' ? '0' : num
})
},
// C按钮事件处理函数
resetBtn: function() {
this.result = null
this.isClear = false
this.setData({
num: '0',
op: '',
history:''
})
}
})
来源:https://blog.csdn.net/weixin_52203618/article/details/124063272
标签:微信小程序,计算器
0
投稿
猜你喜欢
python控制台显示时钟的示例
2023-10-23 12:04:24
基于mysql replication的问题总结
2024-01-29 12:50:52
Python3中str、bytes、bytearray转化
2023-01-11 23:29:30
浅谈keras的深度模型训练过程及结果记录方式
2023-08-11 03:17:46
pandas中去除指定字符的实例
2023-11-29 22:41:35
Python还能这么玩之只用30行代码从excel提取个人值班表
2022-05-03 22:49:45
解决VUE自定义拖拽指令时 onmouseup 与 click事件冲突问题
2024-05-10 14:15:54
一文读懂吞吐量(TPS)、QPS、并发数、响应时间(RT)概念
2022-04-06 14:58:25
基于Python+Pygame实现变异狗大战游戏
2021-04-19 09:46:45
详解python代码模块化
2023-08-08 23:08:23
Python自动化爬取天眼查数据的实现
2021-01-10 23:28:27
vuejs使用axios异步访问时用get和post的实例讲解
2024-05-02 16:56:49
Python数据结构之链表详解
2023-07-21 04:16:19
IE 下 href 的 BUG
2008-11-10 12:32:00
Python中的集合一起来学习一下
2022-05-01 05:06:59
JavaScript版的DateAdd、DateDiff、IsDate函数
2008-01-30 15:35:00
python 网络爬虫初级实现代码
2021-10-02 12:24:13
python 实现图与图之间的间距调整subplots_adjust
2023-10-05 00:52:42
MySQL数据库优化经验详谈(服务器普通配置)第1/3页
2024-01-27 23:30:51
python matplotlib库绘制条形图练习题
2023-04-20 05:49:51