赞
踩
app.json
文件设置window
配置项pages/index/index.wxml
文件data-val
自定义属性,用于区分不同按钮<!--index.wxml--> <!-- 结果区域 --> <view class="result"> <!-- 当前计算式 --> <view class="result-sub">{{sub}}</view> <!-- 当前计算结果 --> <view class="result-num">{{num}}</view> </view> <!-- 按钮区域 --> <view class="btns"> <!-- 第一行按钮 --> <view> <!-- 清除按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnReset">C</view> <!-- 删除按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnDel">Del</view> <!-- 正负号切换按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnPosNeg">+/-</view> <!-- 除号按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnOpreate" data-val="÷">÷</view> </view> <!-- 第二行按钮 --> <view> <!-- 7按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" data-val="7">7</view> <!-- 8按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" data-val="8">8</view> <!-- 9按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" data-val="9">9</view> <!-- 乘号按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnOpreate" data-val="×">×</view> </view> <!-- 第三行按钮 --> <view> <!-- 4按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" data-val="4">4</view> <!-- 5按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" data-val="5">5</view> <!-- 6按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" data-val="6">6</view> <!-- 减号按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnOpreate" data-val="-">-</view> </view> <!-- 第四行按钮 --> <view> <!-- 1按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" data-val="1">1</view> <!-- 2按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" data-val="2">2</view> <!-- 3按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" data-val="3">3</view> <!-- 加号按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnOpreate" data-val="+">+</view> </view> <!-- 第五行按钮 --> <view> <!-- 0按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" data-val="0">0</view> <!-- .按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" bind:tap="btnDot">.</view> <!-- =按钮 --> <view hover-class="bg" hover-stay-time="60" bind:tap="btnNum" bind:tap="btnCalculate">=</view> </view> </view>
pages/index/index.wxss
文件page
样式result
、result-sub
、result-num
.btns
样式类.btns>view
样式类.btns>view >view
样式类.btns> view:last-child> view:first-child
样式类.btns>view:first-child>view:first-child
样式.btns>view>view:last-child
选择器.bg
样式/**index.wxss**/ /* 页面样式 */ page { height: 100vh; display: flex; flex-direction: column; color: rgb(48, 46, 46); } /* 结果区域样式 */ .result{ flex:1; /* 均分手机屏幕,因为flex-direction:colum */ background-color: #f0eeed; position: relative; } /* 当前计算式样式 */ .result-sub{ font-size: 52rpx; position: absolute; bottom: 16vh; right: 3vw; } /* 当前计算结果样式 */ .result-num{ font-size: 100rpx; position: absolute; bottom: 3vh; right: 3vw; } /* 按钮区域样式 */ .btns{ flex:1; display: flex; flex-direction: column; font-size: 48rpx; border-top: 1rpx solid #ccc; border-left: 1rpx solid #ccc; border-right: 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; /* 主轴居中-水平居中 */ } /* 0按钮跨2列 */ .btns> view:last-child> view:first-child{ flex-basis: 50%; } /* 清除按钮样式 */ .btns>view:first-child>view:first-child{ color: rgb(201, 58, 1); } /* 最后一列按钮样式 */ .btns>view>view:last-child{ color: rgba(206, 120, 7, 0.945); } /* 按钮的盘旋样式 */ .bg{ background: #eee; }
utls
目录utils
目录里创建math.js
文件math.js
源码// 精准计算功能,用于解决JavaScript浮点数运算精度不准确的问题 module.exports = { add: function(a, b) { var r1, r2, m try { r1 = a.toString().split('.')[1].length } catch (e) { r1 = 0 } try { r2 = b.toString().split('.')[1].length } catch (e) { r2 = 0 } m = Math.pow(10, Math.max(r1, r2)) return (a * m + b * m) / m }, sub: function(a, b) { var r1, r2, m, n try { r1 = a.toString().split('.')[1].length } catch (e) { r1 = 0 } try { r2 = b.toString().split('.')[1].length } catch (e) { r2 = 0 } m = Math.pow(10, Math.max(r1, r2)) // 动态控制精度长度 n = (r1 >= r2) ? r1 : r2 return ((a * m - b * m) / m).toFixed(n) }, mul: function(a, b) { var m = 0, s1 = a.toString(), s2 = b.toString() try { m += s1.split('.')[1].length } catch (e) {} try { m += s2.split('.')[1].length } catch (e) {} return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m) }, div: function(a, b) { var t1 = 0, t2 = 0, r1, r2 try { t1 = a.toString().split('.')[1].length } catch (e) {} try { t2 = b.toString().split('.')[1].length } catch (e) {} r1 = Number(a.toString().replace('.', '')) r2 = Number(b.toString().replace('.', '')) return (r1 / r2) * Math.pow(10, t2 - t1) } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。