• vue 实现数字验证码功能


    需求:写了一个 手机发送验证码后 输入固定验证码的功能

     

     封装成一个组件,如下:

    1. <template>
    2. <div class="conts">
    3. <div class="box">
    4. <div class="code_list">
    5. <div :class="[ 'code_item', hideIndex == 0 ? 'code_item-focus' : '', value[0] ? 'shows_shaw' : '', ]">
    6. {{ value[0] }}
    7. div>
    8. <div :class="[ 'code_item', hideIndex == 1 ? 'code_item-focus' : '', value[1] ? 'shows_shaw' : '', ]">
    9. {{ value[1] }}
    10. div>
    11. <div :class="[ 'code_item', hideIndex == 2 ? 'code_item-focus' : '', value[2] ? 'shows_shaw' : '', ]">
    12. {{ value[2] }}
    13. div>
    14. <div :class="[ 'code_item', hideIndex == 3 ? 'code_item-focus' : '', value[3] ? 'shows_shaw' : '', ]">
    15. {{ value[3] }}
    16. div>
    17. div>
    18. <input v-model="value" class="field-input" type="number" maxlength="4" @input="input()" @focus="focus()" @blur="blur()" />
    19. div>
    20. div>
    21. template>
    22. <script>
    23. export default {
    24. data() {
    25. return {
    26. value: '',
    27. index: 0,
    28. hideIndex: null,
    29. show_box: false,
    30. }
    31. },
    32. methods: {
    33. input() {
    34. const v = this.value.replace(/[^\d]/g, '');
    35. this.value = v;
    36. this.index = v.length;
    37. this.hideIndex = v.length;
    38. this.$emit('getPassword', v);
    39. },
    40. // 获取焦点
    41. focus() {
    42. this.hideIndex = this.index;
    43. this.show_box = true;
    44. },
    45. // 失去焦点
    46. blur() {
    47. this.hideIndex = null;
    48. this.show_box = false;
    49. },
    50. },
    51. }
    52. script>
    53. <style lang="less">
    54. .conts {
    55. width: 100%;
    56. height: 100%;
    57. display: flex;
    58. align-items: center;
    59. justify-content: center;
    60. text-align: center;
    61. .box {
    62. position: relative;
    63. width: 85%;
    64. overflow: hidden;
    65. .code_list {
    66. display: flex;
    67. justify-content: space-between;
    68. border: 1px solid transparent;
    69. padding: 5px;
    70. border-radius: 3px;
    71. }
    72. }
    73. .field-input {
    74. box-sizing: border-box;
    75. position: absolute;
    76. top: 0;
    77. right: 0;
    78. width: 100%;
    79. height: 59px;
    80. padding: 0;
    81. border: none;
    82. outline: none;
    83. opacity: 0;
    84. background: transparent;
    85. }
    86. }
    87. .shows_shaw {
    88. border: 1px solid #0187fb !important;
    89. }
    90. .code_item {
    91. box-sizing: border-box;
    92. width: 59px;
    93. height: 59px;
    94. line-height: 59px;
    95. font-size: 24px;
    96. text-align: center;
    97. font-weight: 400;
    98. background-color: #f2f5f4;
    99. border: 1px solid transparent;
    100. border-radius: 4px;
    101. margin-right: 7px;
    102. &:last-child {
    103. margin-right: 0;
    104. }
    105. }
    106. .code_item-focus {
    107. border-color: #0187fb;
    108. &::before {
    109. content: "";
    110. display: block;
    111. width: 2px;
    112. height: 24px;
    113. margin: 17px auto;
    114. background: #0187fb;
    115. animation: blink 1s steps(1) infinite;
    116. }
    117. }
    118. @keyframes blink {
    119. 50% {
    120. opacity: 0;
    121. }
    122. }
    123. style>

    引用

    1. "getPassword">
    2. /引入
    3. import roomPassword from '@/components/roomPassword'
    4. //方法
    5. getPassword(val) {
    6. console.log('val', val);
    7. },

  • 相关阅读:
    C++——编译和链接原理笔记
    上海亚商投顾:沪指高开低走涨0.45% 大消费王者归来
    第二章 ClickHouse架构设计
    6.RokcketMQ消息重试与死信队列
    学院打卡第十五天
    GBase 8c V3.0.0数据类型——条件表达式函数
    JavaScript中遍历对象的方法
    Linux编译器-gcc/g++使用
    深扒,Java性能调优手册:编程+多线程+JVM+设计模式+数据库+实战
    oracle使用正则表达式REGEXP_SUBSTR提取XML里面的内容
  • 原文地址:https://blog.csdn.net/yuan_jlj/article/details/133311554