
wxml
<view class="page_box" catchtap="hindKeyboard">
<view class="input_view" catchtap="hindKeyboard">
<text class="title">消费金额:</text>
<view class="input_box" catchtap="showKeyboard">
<text class="input_label">¥</text>
<view class="content" wx:if="{{content.length}}">
<view wx:for="{{content}}" wx:key="index" data-str-index="{{index + 1}}" catchtap="getStrPosition">
<view class="number-block">{{item}}<view class="cursor cursor-insert" wx:if="{{cursorIndex === index + 1}}"></view></view>
</view>
</view>
<view class="cursor" wx:if="{{!cursorIndex}}"></view>
<text class="default-content" wx:if="{{!content.length}}">{{defaultContent}}</text>
</view>
</view>
<view class="keyboard {{keyShow&&'hind_box'}}">
<view class="key_box">
<text class="keys {{index%3==0&&'border0'}} {{(index==9||index==11)&&'bg_color'}}" wx:for='{{KeyboardKeys}}' wx:key="index" catchtap="keyTap" data-keys="{{item}}">{{item}}</text>
</view>
</view>
</view>
wxss
page{
background: #f3f7f7;
height: 100%;
}
.page_box{
width: 100%;
height: 100%;
background: #f3f7f7;
overflow: hidden;
}
.input_view{
width: 700rpx;
height:500rpx;
background: #fff;
margin: 25rpx auto;
border-radius: 10rpx;
padding: 40rpx;
box-sizing: border-box;
}
.title{
display: block;
line-height: 90rpx;
font-size:30rpx;
color: #aaa;
}
.input_box{
display: flex;
align-items: center;
padding: 20rpx 0;
height: 90rpx;
border-bottom: 1px solid #efefef;
}
.input_label{
font-size: 35rpx;
font-weight: bold;
margin-right: 5rpx;
}
.content{
display: flex;
font-size: 80rpx;
line-height: 90rpx;
font-weight: 700;
}
.number-block{
position: relative;
}
.cursor-insert{
position: absolute;
top: 0rpx;
right: -1rpx;
}
.cursor{
width: 2rpx;
height: 90rpx;
background: #666;
border-radius: 6rpx;
animation: twinkling 0.9s infinite ;
}
@-webkit-keyframes twinkling{
0%{
background: #fff;
}
100%{
background: #666;
}
}
.default-content{
color: #999;
font-size: 38rpx;
font-weight: 600;
}
.keyboard{
height: 0;
width: 100%;
background: #fff;
position: fixed;
bottom: 0;
left: 0;
transition:height 0.3s;
}
.hind_box{
height: 440rpx;
}
.key_box{
overflow: hidden;
}
.keys{
box-sizing: border-box;
float: left;
width: 250rpx;
height: 90rpx;
text-align: center;
line-height: 90rpx;
border-left: 1px solid #eee;
border-top: 1px solid #eee;
}
.border0{
border-left: 0;
}
.bg_color{
background: #ccc;
}
js
Page({
data: {
content: [],
defaultContent: '请输入消费金额',
KeyboardKeys: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '0', '<'],
keyShow: true,
cursorIndex: '',
contentLengthMax: 6
},
hindKeyboard() {
this.setData({ keyShow: false });
},
showKeyboard() {
this.setData({ keyShow: true });
},
getStrPosition(e) {
let { strIndex } = e.currentTarget.dataset
this.setData({ cursorIndex: strIndex })
},
keyTap(e) {
let { keys } = e.currentTarget.dataset,
content = this.data.content.join(''),
strLen = content.length,
{ cursorIndex, contentLengthMax } = this.data
switch (keys) {
case '.':
if (strLen < contentLengthMax && content.indexOf('.') === -1) {
content.length < 1 ? content = '0.' : content += '.'
}
break
case '<':
if(cursorIndex > 0 && cursorIndex !== strLen) {
this.data.content.splice(cursorIndex - 1, 1)
content = this.data.content.join('')
}else{
content = content.substr(0, content.length - 1)
}
if(!strLen || cursorIndex === strLen){
this.setData({ cursorIndex: '' })
}
if(content[0] === '0' && content[1] !== '.') {
content = content.substr(1, content.length - 1)
}
if(content[0] === '.') {
content = '0' + content
}
break
default:
let spotIndex = content.indexOf('.')
if(content[0] === '0' && content[1] !== '.') {
content = content.substr(1, content.length - 1)
}
if (strLen < contentLengthMax && (spotIndex === -1 || strLen - spotIndex !== 3)) {
content += keys
}
break
}
this.setData({ content: content.split('') })
},
})
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本