vue 支付密码组件

vue 支付密码组件

我们要做一个类似京东、支付宝支付时的6位密码组件,效果如下。

专门为PC端写的。移动端的很多框架基本都已经有了,而且实现逻辑会比pc简单,因为是移动端一般配单独键盘,甚至不需要input框,不用考虑鼠标,所以对焦点的处理会简单一些。而且移动端没有password autocomplete 的烦心事。

pc 端如果使用 input password 组件的话(参考文章:https://www.jianshu.com/p/03d2a4744258 ),chrome无论怎样都会弹出密码管理窗口。如使用autocomplete=new-password虽然不会自动填充,但还是会弹出密码管理窗口。所以我们使用input text/tel 组件,然后用•或*给盖住。

Vue 整体代码如下:

1<template> 2 <div :id="`ids_${id}`" class="m-password-input"> 3 <div v-for="(v, i) in 6" :key="i" class="box-input"> 4 <div v-show="pwdList[i]" class="u-dot" @click="changePwd"></div> 5 <input 6 :key="i" 7 v-model="pwdList[i]" 8 type="tel" 9 readonly 10 onfocus="this.removeAttribute('readonly');" 11 maxlength="1" 12 autocomplete="new-password" 13 class="u-input" 14 @input="changeInput" 15 @click="changePwd" 16 @keyup="keyUp($event)" 17 @keydown="oldPwdList = pwdList.length" 18 > 19 </div> 20 </div> 21</template> 22 23<script> 24export default { 25 props: { 26 id: { 27 type: Number, 28 default: 1 // 当一个页面有多个密码输入框时,用id来区分 29 } 30 }, 31 data() { 32 return { 33 pwdList: [], 34 pwdListReal: [], 35 oldPwdList: [], 36 isDelete: false, 37 ipt: '' 38 } 39 }, 40 mounted() { 41 this.ipt = document.querySelectorAll(`#ids_${this.id} .u-input`) 42 }, 43 methods: { 44 keyUp(ev) { 45 let index = this.pwdList.length 46 if (!index) return 47 if (ev.keyCode === 8) { 48 this.isDelete = true 49 if (this.oldPwdList === this.pwdList.length) { 50 if (index === this.pwdList.length) { 51 this.pwdList.pop() 52 } 53 index-- 54 } else { 55 index > 0 && index-- 56 } 57 this.ipt[index].focus() 58 } else if (this.isDelete && index === this.pwdList.length && /^\d$/.test(ev.key)) { 59 this.isDelete = false 60 this.pwdList.pop() 61 this.pwdList.push(ev.key) 62 this.ipt[this.pwdList.length] && this.ipt[this.pwdList.length].focus() 63 } 64 this.$emit('get-pwd', this.pwdList.join('')) 65 }, 66 changePwd() { 67 let index = this.pwdList.length 68 index === 6 && index-- 69 this.ipt[index].focus() 70 }, 71 changeInput() { 72 let index = this.pwdList.length 73 const val = this.pwdList[index - 1] 74 if (!/[0-9]/.test(val)) { 75 this.pwdList.pop() 76 return 77 } 78 if (!val) { 79 this.pwdList.pop() 80 index-- 81 if (index > 0) this.ipt[index - 1].focus() 82 } else { 83 if (index < 6) this.ipt[index].focus() 84 } 85 } 86 } 87} 88</script> 89 90<style lang="scss" scoped> 91.m-password-input { 92 display: flex; 93 border-radius: 5px; 94 padding: 10px 0; 95 border: 1px solid #cccccc; 96 position: relative; 97 margin-left: 1px; 98 .box-input { 99 display: inline-block; 100 position: relative; 101 border-right: 1px solid #cccccc; 102 &:last-child{ 103 border-right:0; 104 } 105 .u-dot { 106 position: absolute; 107 top: 0; 108 right: 0; 109 bottom: 0; 110 left: 0; 111 display: flex; 112 align-items: center; 113 justify-content: center; 114 font-size: 40px; 115 } 116 .u-input { 117 text-align: center; 118 font-size: 30px; 119 float: left; 120 width: 40px; 121 height: 20px !important; 122 color: transparent; 123 caret-color: #333333; 124 outline: unset; 125 border: none; 126 border-right: 1px solid #cccccc; 127 &:last-child{ 128 border-right:0; 129 } 130 } 131 } 132} 133</style>

使用:

1<password-input @get-pwd="getPwd" /> 2import PasswordInput from '@/components/PasswordInput' 3methods: { 4 getPwd(password) { 5 console.log(password) 6 } 7 }

参考文章:

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid