前端实现微信小程序 支付密码输入框

想要达到的效果如下所示:
在这里插入图片描述
index.wxml 文件代码

<!-- 输入密码框 -->
<view class="input">
   <view class="input-item" wx:for="{{6}}" wx:key="index" data-index="{{index}}">
     <!-- 实际显示框 -->
     <view class="input-value">{{password[index]}}</view>
     <!-- 光标显示 -->
     <!-- <view class="focus {{index === focusIndex ? 'show': 'hide'}}"></view> -->
   </view>
   <!-- 实际的输入框 -->
   <input class="input-password" maxlength="6" bindinput="setValue" bindblur="inputBlur" type="number" focus="{{focus}}"></input>
 </view>

index.wxss文件代码

/* 密码输入框 */
.input {
  margin-top: 70rpx;
  /* padding: 0 60rpx; */
  position: relative;
}
.input-item {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 40px;
  /* border-bottom: 2rpx solid #333;*/  /* 下划线效果*/ 
  background: rgba(0, 0, 0, 0.04); /* 灰色框效果 */ 
  border-radius: 4px;
}
.input-item:not(:first-child) {
  margin-left: 8px;
}
.input-value {
  display: inline-block;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 28rpx;
}
.input-password {
  position: absolute;
  left: -360rpx;
  top: 0;
  height: 90rpx;
  width: 880rpx;
  /* opacity: 0; */ /* 真机opacity 无效 */ 
  
  /* 解决真机opacity 无效的问题 */ 
  background-color:rgba(255, 255, 255, 0.1); 
  color: rgba(255, 255, 255, 0.1);
}
.focus {
  width: 2rpx;
  height: 50rpx;
  background-color: #333;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.show {
  display: block;
  animation: blink 1s linear infinite;
}
.hide {
  display: none;
}

@keyframes blink {
  0%, 50% {
    opacity: 1;
  }

  50.01%, to {
    opacity: 0;
  }
}

index.js代码

setValue(e) {
 // 设置光标
  var value = e.detail.value
  this.setData({
    value: value,
    focusIndex: value.length,
    focus: value.length < 6,
    password: '*'.repeat(value.length)
  })
  // if (value.length == 6) {
  //   console.log(value, 'sssssssssssssssss')
  // }
},
inputBlur(e) {
  if (e.detail.value.length === 6) {
    this.triggerEvent('complated', {
      value: e.detail.value
    })
  }
},
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值