小程序模板网

微信小程序之支付密码输入demo

发布时间:2018-08-17 08:59 所属栏目:小程序开发教程

在小程序中实现支付密码的输入,要解决几个问题: 
1、小程序要想唤起键盘,必须要借助input控件。通过input控件和其属性focus来唤起和隐藏输入键盘。

2、要让input控件不可见。让光标和输入的字符都不可见,这里是把input控件定位到左边不可见范围。

先看实现后的效果图:

实现demo代码:

1、页面代码

 

				
  1. <view catchtap='showInputLayer' class="btn_pay">立即支付</view>
  2. <!-- 密码输入框 -->
  3. <view wx:if='{{showPayPwdInput}}'>
  4. <view class='bg_layer'></view>
  5. <view class='input_main'>
  6. <view class='input_title'>
  7. <view class='input_back' catchtap='hidePayLayer'><text></text></view>
  8. <text>输入支付密码</text>
  9. </view>
  10. <view class='input_tip'><text>使用会员卡余额支付需要验证身份,验证通过后才可进行支付。</text></view>
  11. <view class='input_row' catchtap='getFocus'>
  12. <view class='pwd_item' wx:for='{{6}}' wx:key='item' wx:for-index='i'>
  13. <text wx:if='{{pwdVal.length>i}}'></text>
  14. </view>
  15. </view>
  16. <view class='forget_pwd' catchtap='hidePayLayer'>忘记密码</view>
  17. <input class='input_control' password type='number' focus='{{payFocus}}' bindinput='inputPwd' maxlength='6'/>
  18. </view>
  19. </view>

2、js代码

 

				
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4.  
  5. Page({
  6. data: {
  7. showPayPwdInput: false, //是否展示密码输入层
  8. pwdVal: '', //输入的密码
  9. payFocus: true, //文本框焦点
  10. },
  11. onLoad: function () {
  12. this.showInputLayer();
  13. },
  14. /**
  15. * 显示支付密码输入层
  16. */
  17. showInputLayer: function(){
  18. this.setData({ showPayPwdInput: true, payFocus: true });
  19. },
  20. /**
  21. * 隐藏支付密码输入层
  22. */
  23. hidePayLayer: function(){
  24.  
  25. var val = this.data.pwdVal;
  26.  
  27. this.setData({ showPayPwdInput: false, payFocus: false, pwdVal: '' }, function(){
  28. wx.showToast({
  29. title: val,
  30. })
  31. });
  32.  
  33. },
  34. /**
  35. * 获取焦点
  36. */
  37. getFocus: function(){
  38. this.setData({ payFocus: true });
  39. },
  40. /**
  41. * 输入密码监听
  42. */
  43. inputPwd: function(e){
  44. this.setData({ pwdVal: e.detail.value });
  45.  
  46. if (e.detail.value.length >= 6){
  47. this.hidePayLayer();
  48. }
  49. }
  50. })

3、样式

 

				
  1. .btn_pay{
  2. margin: 100rpx auto; width: 600rpx; height: 100rpx; line-height: 100rpx; border-radius: 100rpx;
  3. background-color: #d3a95a; color: #fff; font-size: 36rpx; text-align: center;
  4. }
  5. /* 支付密码css start */
  6. .bg_layer{
  7. position: fixed; left: 0; top: 0; bottom: 0; right: 0;
  8. background-color: rgba(0, 0, 0, 0.6); z-index: 9998;
  9. }
  10. .input_main{
  11. position: fixed; left: 0; bottom: 500rpx; width: 100%; height: 394rpx;
  12. background-color: #fff; z-index: 9999;
  13. }
  14. .input_title{
  15. width: 100%; height: 90rpx; line-height: 90rpx; text-align: center;
  16. font-size: 32rpx; border-bottom: 1rpx solid #e2e2e2;
  17. }
  18. .input_back{
  19. position: absolute; left: 0; top: 0;
  20. width: 80rpx; height: 90rpx; display: flex; justify-content: center; align-items: center;
  21. }
  22. .input_back text{
  23. width: 20rpx;
  24. height: 20rpx;
  25. background-color: white;
  26. border: 1rpx solid #aaa;
  27. border-width: 5rpx 0 0 5rpx;
  28. transform: rotate(-45deg);
  29. }
  30.  
  31. .input_tip{ margin: 30rpx; font-size: 24rpx; color: #888; }
  32.  
  33. /* 密码掩码模拟 */
  34. .input_row{
  35. width: 690rpx; margin: 0 auto; height: 98rpx; position: relative;
  36. display: flex; align-items: center; border: 1rpx solid #e2e2e2; border-radius: 20rpx;
  37. }
  38. .input_row .pwd_item{
  39. flex: 1; display: flex; align-items: center; justify-content: center;
  40. height: 100%; border-right: 1rpx solid #e2e2e2; position: relative;
  41. }
  42. .pwd_item:nth-last-of-type(1) { border-right: 0; }
  43. .pwd_item text {
  44. width: 30rpx; height: 30rpx; border-radius: 30rpx; background-color: #555;
  45. }
  46.  
  47.  
  48. .forget_pwd{
  49. float: right; margin: 30rpx; width: 100rpx; text-align: right; font-size: 24rpx; color: #ff7800;
  50. }
  51.  
  52. /* 文本输入框位置: 设置到左边隐藏 */
  53. .input_control {
  54. position: relative; left: -300rpx; bottom: 0; width: 100rpx; height: 100rpx;
  55. }


易优小程序(企业版)+灵活api+前后代码开源 码云仓库:starfork
本文地址:https://www.eyoucms.com/wxmini/doc/course/24727.html 复制链接 如需定制请联系易优客服咨询:800182392 点击咨询
QQ在线咨询