微信小程序数字键盘(仿微信转账键盘)

微信小程序input自带数字输入键盘,不过是直接调用的系统键盘,无法个性化。

代码中使用使用了Vant WeappVant UI小程序版,这里就不介绍相关安装说明了,大家自行安装Vant Weapp

json 用到的组件

javascript 复制代码
{
  "usingComponents": {
    "van-cell": "@vant/weapp/cell/index",
    "van-button": "@vant/weapp/button/index",
    "van-popup": "@vant/weapp/popup/index",
    "van-field": "@vant/weapp/field/index",
    "van-row": "@vant/weapp/row/index",
    "van-col": "@vant/weapp/col/index"
  }
}

wxml 结构

html 复制代码
<van-cell title="分数" value="{{score || '点击打分'}}" bindtap="tapScore" />

<!-- 打分键盘 -->
<van-popup
  show="{{ keyboardShow }}"
  position="bottom"
  custom-style="height: 508rpx;"
  bind:close="onClose"
>
  <view class="keyborad">
    <view class="input">
      <van-field
        value="{{ value }}"
        custom-style="border: 2prx solid #dcdee0"
        placeholder="请选择分数"
        disabled
      />
    </view>
    <view class="number-keyboard">
      <van-row class="number" gutter="10">
        <van-col
          wx:for="{{number}}"
          wx:key="index"
          data-key="{{item}}"
          custom-class="number-item"
          span="{{item === 0 ? '16' : '8'}}"
          bindtap="tapNumber">
          <view class="number-item__key tap-key">{{item}}</view>
        </van-col>
      </van-row>
      <view class="operation">
        <view class="del tap-key" bindtap="tapBksp">
          <image class="del-icon" src="/assets/backspace.png"></image>
        </view>
        <view class="confirm tap-key" bindtap="confirm">确定</view>
      </view>
    </view>
  </view>
</van-popup>

js 内容

javascript 复制代码
Page({
  data: {
    score: '',
    keyboardShow: false,
    value: '',
    number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '.']
  },
  tapScore() {
    this.setData({
      keyboardShow: true
    })
  },
  onClose() {
    this.setData({
      keyboardShow: false
    })
  },
  // number点击
  tapNumber(e) {
    const { key } = e.currentTarget.dataset
    let { value } = this.data
    value += key
    /**
     * 限制输入
     * 开头不能是小数点
     * 只能有一位小数点
     * 0开头只能跟小数点
     * 小数点后限制一位
     */
    value = String(value)
            .replace(/^\./g, '')
            .replace('.', '$#$')
            .replace(/\./g, '')
            .replace('$#$', '.')
            .replace(/^00$/, '0')
            .replace(/^0(\d)/g, '$1')
            .replace(/^(-)*(\d+)\.(\d{1}).*$/, '$1$2.$3')

    this.setData({
      value
    })
  },
  // 退格
  tapBksp() {
    let { value } = this.data
    value = String(value)
    value = value.substr(0, value.length - 1)
    this.setData({
      value
    })
  },
  // 确定
  confirm() {
    let { value } = this.data
    this.setData({
      score: value,
      keyboardShow: false
    })
  },
})

wxss 样式

css 复制代码
.keyborad .number-keyboard {
  display: flex;
  background-color: #ebedf0;
  padding: 20rpx 20rpx 0 20rpx;
}
.keyborad .number-keyboard .tap-key:active {
  opacity: 0.8;
}
.keyborad .number-keyboard .number {
  flex: 1;
}
.keyborad .number-keyboard .number .number-item {
  margin-bottom: 20rpx;
}
.keyborad .number-keyboard .number .number-item .number-item__key {
  background-color: #fff;
  text-align: center;
  height: 80rpx;
  line-height: 80rpx;
  border-radius: 8rpx;
  font-size: 32rpx;
  font-weight: 700;
}
.keyborad .number-keyboard .operation {
  width: 200rpx;
  display: flex;
  flex-direction: column;
  margin: 0 0 20rpx 20rpx;
}
.keyborad .number-keyboard .operation .del {
  height: 80rpx;
  text-align: center;
  margin-bottom: 20rpx;
  background-color: #fff;
  border-radius: 8rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}
.keyborad .number-keyboard .operation .del .del-icon {
  width: 40rpx;
  height: 40rpx;
}
.keyborad .number-keyboard .operation .confirm {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #04943f;
  border-radius: 8rpx;
  color: #fff;
  font-size: 36rpx;
}
.keyborad .van-field__control--disabled {
  color: #666 !important;
}
相关推荐
乘风gg6 分钟前
多 Agent 不是万能的!搞懂这 5 个原则,少走 1 年弯路!
前端·agent·ai编程
猩猩程序员40 分钟前
Vercel 推出 Agent 框架 Eve:让 AI Agent 像写 Web 应用一样简单
前端
爱读源码的大都督1 小时前
Claude Code源码分析(三):为什么系统提示词中需要有tools呢?
前端·人工智能·后端
爱勇宝1 小时前
Claude Code 被曝暗藏“隐形检测”代码:封代理不是最可怕的,可怕的是你根本不知道它在干什么
前端·后端·程序员
小牛不牛的程序员1 小时前
我用 Claude Code 半天撸完了一个完整网站,AI 编程到底提升了多少效率?
前端
东风破_1 小时前
JavaScript 面试常考的字符串算法:从反转字符串到回文判断
前端·javascript
ITOM运维行者2 小时前
从零搭建企业级服务器监控体系:踩坑实录与架构设计
前端·后端
monologues2 小时前
深入 Vue 3 源码:响应式系统的精妙设计与编译优化
前端
hunterandroid2 小时前
Paging 3 分页:从手动分页到声明式加载
前端
用户4099322502122 小时前
Vue状态管理入门第四章:组合式store和SSR风险
前端·vue.js·后端