yi-code.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view class="yi-code" :style="'width: '+width+'rpx;'">
  3. <view class="yi-code-show" :style="'width: '+width+'rpx;'">
  4. <block v-for="i in numberArr">
  5. <view
  6. :class="'yi-code-show-item' + ((codes.length === i && isFocus) ? ' yi-code-show-active' : '')"
  7. :style="type !== 'block' ? (type === 'line' ? 'border-top: 0; border-left: 0; border-right: 0; border-radius: 0; border-width: 2px;' : 'border-style: dashed;') : ''"
  8. >
  9. {{showVal(codes[i])}}
  10. </view>
  11. </block>
  12. </view>
  13. <view class="yi-code-hide" :style="'width: '+(width * 2)+'rpx;left: -'+ width +'rpx;'">
  14. <input style="height: 100%;" v-model="value" :style="'width: '+(width * 2)+'rpx;'" :focus="focus" @focus="onFocus" @blur="onBlur" :type="inputType" @input="onChange" :maxlength="maxlength"/>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. props: {
  21. /**
  22. * @description 宽度 rpx
  23. */
  24. width: {
  25. type: Number,
  26. default: 500,
  27. },
  28. /**
  29. * @description 是否自动聚焦
  30. */
  31. focus: {
  32. type: Boolean,
  33. default: true,
  34. },
  35. /**
  36. * @description 隐藏字符(密码效果)
  37. */
  38. hide: {
  39. type: String,
  40. default: '',
  41. },
  42. /**
  43. * @description 验证码长度
  44. */
  45. maxlength: {
  46. type: Number,
  47. default: 6
  48. },
  49. /**
  50. * @description 样式类型 block 方框, dashed 虚线方框,line 线
  51. */
  52. type: {
  53. type: String,
  54. default: 'block'
  55. },
  56. /**
  57. * @description text | number
  58. */
  59. inputType: {
  60. type: String,
  61. default: 'number'
  62. },
  63. },
  64. created() {
  65. let arr = [];
  66. for(let i = 0; i < this.$props.maxlength; i++){
  67. arr.push(i);
  68. }
  69. this.numberArr = arr;
  70. },
  71. data(){
  72. return {
  73. numberArr: [], //
  74. codes: [],
  75. value: '',
  76. isFocus: false
  77. }
  78. },
  79. methods: {
  80. clear(){
  81. this.codes = []
  82. this.value = ''
  83. },
  84. onFocus(){
  85. this.isFocus = true
  86. },
  87. onBlur(){
  88. this.isFocus = false
  89. },
  90. showVal(v){
  91. return v ? (this.hide || v) : '';
  92. },
  93. onChange(e){
  94. let str = e.detail.value || e.target.value
  95. if(str){
  96. this.codes = String(str).split('');
  97. this.$emit('onChange', str);
  98. if(this.codes.length === this.$props.maxlength){
  99. this.$emit('onComplete', str);
  100. }
  101. }else{
  102. this.codes = [];
  103. this.$emit('onChange', '');
  104. }
  105. }
  106. }
  107. }
  108. </script>
  109. <style lang="scss">
  110. .yi-code {
  111. position: relative;
  112. overflow: hidden;
  113. text-align: unset;
  114. .yi-code-show {
  115. display: flex;
  116. justify-content: space-between;
  117. align-items: center;
  118. .yi-code-show-item {
  119. box-sizing: border-box;
  120. width: 80rpx;
  121. height: 80rpx;
  122. border: 1px solid #777;
  123. border-radius: 6rpx;
  124. display: flex;
  125. align-items: center;
  126. justify-content: center;
  127. }
  128. .yi-code-show-active {
  129. border-color: #ff5500;
  130. border-width: 2px;
  131. animation: myfirst 600ms infinite;
  132. @keyframes myfirst
  133. {
  134. 0% {opacity: 0.1}
  135. 100% {opacity: 1}
  136. }
  137. }
  138. }
  139. .yi-code-hide{
  140. position: absolute;
  141. z-index: 99;
  142. left: 0;
  143. top: 0;
  144. height: 80rpx;
  145. opacity: 0;
  146. text-align: unset;
  147. input{
  148. height: 80rpx;
  149. text-align: unset;
  150. }
  151. }
  152. }
  153. </style>