zoukankan      html  css  js  c++  java
  • 微信小程序之自定义验证码or密码输入框

    业务场景:

      小程序中有地方用到需要自定义输入验证码或者密码的地方,例如:

    或者

     

    这类场景。

    需求:n个方框为输入框,框中有光标,且光标随着输入字符移动,输入完成后隐藏输入框/自动校验等

    实现:方框用div模拟输入框,然后一个输入框覆盖在方框div上,光标用动画实现

    伪代码:

    wxml文件:

    <view class="input">
      <view class="input-item" wx:for="{{4}}" 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="4" bindinput="setValue" bindblur="inputBlur" type="number" focus="{{focus}}"></input>
    </view>

    js文件:

    Component({
      /**
       * 组件的初始数据
       */
      data: {
        focusIndex: 0, // 光标所在位置
        value: '', // 实际输入的值
        focus: true, // 是否获得焦点
        password: '', //替换显示的值*
      },
      /**
       * 组件的方法列表
       */
      methods: {
        setValue (e) {
          // 设置光标
          var value = e.detail.value
          this.setData({
            value: value,
            focusIndex: value.length,
            focus: value.length < 4,
            password: '*'.repeat(value.length)
          })
        },
        inputBlur (e) {
          if (e.detail.value.length === 4) {
            this.triggerEvent('complated', {
              value: e.detail.value
            })
          }
        }
      }
    })

    wxss文件:

    .input {
      margin-top: 70rpx;
      padding: 0 60rpx;
      position: relative;
    }
    .input-item {
      position: relative;
      display: inline-block;
       90rpx;
      height: 90rpx;
      border-bottom: 2rpx solid #333;
    }
    .input-item:not(:first-child) {
      margin-left: 26.67rpx;
    }
    .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;
       880rpx;
      opacity: 0;
    }
    .focus {
       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;
      }
    }

    大致这样,password可以做明文替换为*

     骚操作就是将input定位在自定义div上面,并且将input的宽度拉大,然后向左移动整个模拟div的宽度,隐藏掉它。

  • 相关阅读:
    转《编程之美——微软技术面试心得》勘误表
    第一次 学习使用 智能指针
    test _todel
    (转 todo阅读)Android 官方博客 Android应用程序的内存分析(翻译)
    msdn snmp trap 研究可否 重入 转《Multiple Trap Registrations》
    wpbars在博客园开博客了
    创业失败的10个教训总结
    winform 的一种登录方法。
    快速建立Subversion
    (转)SQL Server 按某一字段分组取最大(小)值所在行的数据
  • 原文地址:https://www.cnblogs.com/xuejiangjun/p/12704059.html
Copyright © 2011-2022 走看看