uniapp怎么自定义验证码输入框并隐藏光标

其他教程   发布日期:2023年06月27日   浏览次数:627

这篇文章主要讲解了“uniapp怎么自定义验证码输入框并隐藏光标”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“uniapp怎么自定义验证码输入框并隐藏光标”吧!

一. 前言

  1. 点击输入框唤起键盘,蓝框就相当于input的光标,验证码输入错误或者不符合格式要求会将字体以及边框改成红色提示,持续1s,然后清空数据,恢复原边框样式;

  2. 5位验证码输入完毕,点击页面其他位置,隐藏键盘;这时如果发现验证码有误,再次点击输入框又唤起键盘,也能正常删除数字(这里其实做的时候遇到了bug,再次聚焦不能删除错误数字,下文会讲到)。

二. 实现思路

具体实现思路:

  • 将input标签相对于父元素做绝对定位,与父元素左边距设置为负的本身宽度即可(position: absolute; top: 0; left:-100%; width: 100%; height: 100%;)。

  • 动态去设置input的focus属性。

  • input同级使用for循环去创建5个正方形的view标签。

  • 给input同级创建的view标签绑定点击事件,在点击事件方法实现中去设置input的focus属性为true,即可弹出键盘。

  • 在键盘输入的时候,即可触发input属性的一系列方法,利用v-model双向绑定,将input输入的值赋值给循环的view方框即可。

  • 这样input也就不在屏幕中,但是又可以触发input的事件。

总的来说就是,使用for循环去创建5个正方形的view标签,然后创建一个input标签,type=tel,最大输入长度为5(根据需求来设置),再将input伪隐藏掉,获取的值分别放到5个view中展示。

验证码失败后利用v-model双向绑定,清空输入的值,增加错误提示文字和边框样式。

三. 代码实现

父组件

  1. <uni-popup ref="codeInputPopup" background-color="#fff" :mask-click ="false" type="center">
  2. <CodeInput
  3. :codeLength="5"
  4. :disabled="codeBtnDisabled"
  5. @codeInputClose="codeInputClose"
  6. @submitGoodCode="submitGoodCode"
  7. />
  8. </uni-popup>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. intviation_code:'', //邀请码
  14. codeBtnDisabled: false //防止接口请求还未返回数据,用户多次点击
  15. }
  16. },
  17. methods: {
  18. // 提交邀请码
  19. async submitGoodCode(intviation_code){
  20. this.codeBtnDisabled = true
  21. this.intviation_code = intviation_code
  22. const response = await this.$api.post('/ebapi/pink_api/secret_intviation_check', {
  23. code: intviation_code
  24. })
  25. if(response.code === 200){
  26. this.codeBtnDisabled = false
  27. this.$refs.codeInputPopup.close()
  28. }else{
  29. this.codeBtnDisabled = false
  30. this.$refs.codeInputPopup.close()
  31. this.$api.msg(response.msg)
  32. }
  33. },
  34. codeInputClose(){
  35. this.$refs.codeInputPopup.close()
  36. this.codeBtnDisabled = false
  37. }
  38. }
  39. </script>

子组件

  1. <template>
  2. <view>
  3. <view class="code-popup-top">
  4. <view class="code-title">请输入商品邀请码</view>
  5. <view class="close-icon" @click="codeInputClose">
  6. <uni-icons type="closeempty" size="30" color="#999999" />
  7. </view>
  8. </view>
  9. <!-- 错误提示 -->
  10. <view class="code_errow" v-if="codeColor == '#ff0000'&& !isNum">邀请码必须{{ codeLength }}位数</view>
  11. <view class="code_errow" v-if="codeColor == '#ff0000'&& isNum ">邀请码必须是数字</view>
  12. <view class="code_input_con">
  13. <view
  14. v-for="(item, index) in codeLength"
  15. :key="index"
  16. class="code_input_item"
  17. :
  18. @click="focus = true"
  19. >{{ intviation_code[index] && intviation_code[index] || '' }}</view>
  20. <input
  21. class="cinput"
  22. type="tel"
  23. v-model="intviation_code"
  24. :maxlength="codeLength"
  25. :focus="focus"
  26. :cursor="intviation_code.length"
  27. @focus="focus = true "
  28. @blur="focus = false"
  29. />
  30. </view>
  31. <button
  32. :class="['submit_code_btn', disabled ? 'btn_disabled' : '']"
  33. :disabled="disabled"
  34. @click="submitGoodCode"
  35. >确定</button>
  36. </view>
  37. </template>
  38. <script>
  39. export default {
  40. data() {
  41. return {
  42. codeColor: '#313131', //自定义错误码颜色
  43. intviation_code: '', //用户输入的验证码
  44. focus: false, // 动态获取焦点的值
  45. isNum: false,
  46. }
  47. },
  48. props: {
  49. codeLength: {
  50. type: Number,
  51. default: 5,
  52. },
  53. disabled: {
  54. type: Boolean,
  55. default: false,
  56. },
  57. },
  58. methods: {
  59. codeInputClose() {
  60. this.intviation_code = ''
  61. this.$emit('codeInputClose')
  62. },
  63. submitGoodCode() {
  64. if (this.intviation_code.length === this.codeLength) {
  65. if (Number(this.intviation_code)) {
  66. this.$emit('submitGoodCode', this.intviation_code)
  67. } else {
  68. this.isNum = true
  69. this.publicErrorSetting()
  70. }
  71. } else {
  72. this.publicErrorSetting()
  73. }
  74. },
  75. // 输入不符合规范,更改样式并清空
  76. publicErrorSetting() {
  77. this.codeColor = '#ff0000'
  78. setTimeout(() => {
  79. this.intviation_code = ''
  80. this.codeColor = '#313131'
  81. this.isNum = false
  82. }, 1000)
  83. },
  84. },
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. .code-popup-top {
  89. display: flex;
  90. justify-content: space-between;
  91. align-items: center;
  92. margin-bottom: 50upx;
  93. .code-title {
  94. font-size: 34upx;
  95. color: #333;
  96. font-weight: bold;
  97. position: relative;
  98. &::before {
  99. content: '';
  100. position: absolute;
  101. bottom: 0;
  102. width: 40upx;
  103. height: 19upx;
  104. background: linear-gradient(
  105. to right,
  106. rgba(57, 181, 74, 1),
  107. rgba(57, 181, 74, 0.1)
  108. );
  109. }
  110. }
  111. .close-icon {
  112. background: #f2f4f7;
  113. border-radius: 50%;
  114. display: flex;
  115. align-items: center;
  116. justify-content: center;
  117. }
  118. }
  119. .code_errow {
  120. font-size: 30upx;
  121. color: #ff5500;
  122. margin-bottom: 20upx;
  123. }
  124. .submit_code_btn {
  125. width: 100%;
  126. height: 83upx;
  127. line-height: 83upx;
  128. border-radius: 7upx;
  129. background: #39b54a;
  130. color: #fff;
  131. font-size: 31upx;
  132. text-align: center;
  133. margin-top: 45upx;
  134. }
  135. .btn_disabled {
  136. color: rgba(255, 255, 255, 0.5) !important;
  137. background-color: rgba(57, 181, 74, 0.4) !important;
  138. }
  139. .code_input_con {
  140. display: flex;
  141. justify-content: space-around;
  142. position: relative;
  143. .code_input_item {
  144. margin-left: 10upx;
  145. text-align: center;
  146. line-height: 88upx;
  147. border-radius: 14upx;
  148. width: 88upx;
  149. height: 88upx;
  150. font-size: 60upx;
  151. font-weight: bold;
  152. color: #333;
  153. &:last-child {
  154. margin-right: 0;
  155. }
  156. }
  157. /*input隐藏掉*/
  158. .cinput {
  159. position: absolute;
  160. top: 0;
  161. left: -100%;
  162. width: 100%;
  163. height: 100%;
  164. }
  165. }
  166. </style>

四. 过程中遇到的问题

1)input 的type=&lsquo;number&rsquo;, ios手机正常,光标在内容最后,但Android手机光标有时候在内容最前面,导致聚焦内容删不掉。

修改input 的

  1. type = 'tel'
  1. :cursor="intviation_code.length"
, 这样cursor属性才生效,并指定focus时光标的位置在内容最后;
type=&lsquo;tel&rsquo;,也会有个小问题,可以输入一些字符,但是我们的需求只能是数字,所以代码中要做限制。就能解决这个问题了。

这个cursor无效的问题,在h6模式应该是type的原因,我试了在type是number或digit时cursor就无效,text、tel、idcard就有效

2)还有另外一种方法

  • 设置input的type=“number”,就不需要设置光标位置了;然后隐藏input文字和光标,相当于间接隐藏了input框;

  • 用到了css样式设置,

    1. color: transparent; caret-color: transparent;
  • 最主要的还是相对于父元素做绝对定位,与父元素左边距设置为负的本身宽度的一半即可(position: absolute; top: 0; left:-100%; width: 200%; height: 100%;)with: 200%为了增大点击区域,解决Android机型再次唤起键盘不能聚焦,删不掉错误数字的问题。

张鑫旭的CSS改变插入光标颜色caret-color简介及其它变色方法
自我测试CSS : caret-color

  1. <template>
  2. <view>
  3. <input
  4. class="cinput"
  5. type="number"
  6. v-model="intviation_code"
  7. :maxlength="codeLength"
  8. :focus="focus"
  9. @focus="focus = true "
  10. @blur="focus = false"
  11. />
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. intviation_code: '', //商品邀请码
  20. focus: false,
  21. }
  22. },
  23. methods: {}
  24. </script>
  25. <style lang="scss" scoped>
  26. .cinput {
  27. position: absolute;
  28. top: 0;
  29. left: -100%;
  30. width: 200%;
  31. height: 100%;
  32. color: transparent; //输入文字颜色透明
  33. caret-color: transparent !important; //改变插入光标颜色为透明
  34. }
  35. }
  36. // 考虑兼容性
  37. // 浏览器支持caret-color属性,优先使用caret-color(Chrome/Firefox/Opera);其次使用::first-line方法(Safari);最后忽略(如IE)。
  38. @supports (-webkit-mask: none) and (not (caret-color: transparent)) {
  39. .cinput {
  40. color: transparent !important;
  41. }
  42. .cinput::first-line {
  43. color: transparent !important;
  44. }
  45. }
  46. </style>

以上就是uniapp怎么自定义验证码输入框并隐藏光标的详细内容,更多关于uniapp怎么自定义验证码输入框并隐藏光标的资料请关注九品源码其它相关文章!