如图。我们一般做商城类的项目不免会用到支付密码输入框,我研究了下并决定发上来,也当作是自己成长路上的一点小小的记录。本次介绍的是基于vue的项目

html:

<template>
<div class='am_payPwd' :id="`ids_${id}`">
<input type="password"
maxlength="1"
@input="changeInput"
@click="changePwd"
v-model="pwdList[i]"
@keyup="keyUp($event)"
@keydown="oldPwdList = pwdList.length"
class="shortInput"
v-for="(v, i) in 6" :key="i">
</div>
</template>

css:

.am_payPwd {
display: inline-block;
width: 242px;
height: 40px;
border: 1px solid #999;
border-radius: 5px;
padding: 10px 0;
position: relative;
margin-left: 1px;
.shortInput {
text-align: center;
font-size: 20px;
float: left;
width: 40px;
height: 20px;
color: #333;
outline: #ff0067;
&:not(:last-child) {
border-right: 1px solid #999;
}
}
}

js:

  data () {
return {
pwdList: [],
oldPwdList: [],
isDelete: false,
ipt: ''
}
},
props: {
id: String, // 当一个页面有多个密码输入框时,用id来区分
default: '1'
},
mounted () {  
this.ipt = document.querySelectorAll(`#ids_${this.id} .shortInput`)
},
methods: {
keyUp (ev) {
let index = this.pwdList.length
if (!index) return
if (ev.keyCode === 8) {
this.isDelete = true
if (this.oldPwdList === this.pwdList.length) {
if (index === this.pwdList.length) {
this.pwdList.pop()
}
index--
} else {
index > 0 && index--
}
this.ipt[index].focus()
} else if (this.isDelete && index === this.pwdList.length && /^\d$/.test(ev.key)) {
this.isDelete = false
this.pwdList.pop()
this.pwdList.push(ev.key)
this.ipt[this.pwdList.length] && this.ipt[this.pwdList.length].focus()
}
this.$emit('getPwd', this.pwdList.join(''))
},
changePwd () {
let index = this.pwdList.length
index === 6 && index--
this.ipt[index].focus()
},
changeInput () {
let index = this.pwdList.length
let val = this.pwdList[index - 1]
   if (!/[0-9]/.test(val)) { 
this.pwdList.pop()          
return       
}
if (!val) {
this.pwdList.pop()
index--
if (index > 0) this.ipt[index - 1].focus()
} else {
if (index <) this.ipt[index].focus()
}
}
}

如有考虑不周的,请指出

分享一个PC端六格密码输入框写法的更多相关文章

  1. 怎样写一个PC端使用的操盘手软件(用来买卖股票,查看报表,行情)

    我们想写一个操盘手软件,对于操盘而言,首先是快,然后是资料尽可能丰富,最好能看到行情,报表什么的.只是windows上写软件看似基础,实际上都不怎么好弄,用C++开发确实可以实现所有功能,估计光研发费 ...

  2. Supalle-Admin-Layout,一个PC端和手机端都合适用的后台页面模板

    Supalle-Admin-Layout主要使用有Vue.Element-UI.layui-icon,Ajax实现采用Fetch(是有这个打算,不过目前是jQuery.). 源码地址:https:// ...

  3. 分享一个移动端rem布局的适配mixin

    /*================================================================ 以下为基于ip5 宽度320做的适配,标准html{font-si ...

  4. PC端页面适应不同的分辨率的方法 (转载)

    原文地址:https://blog.csdn.net/fengzhen8023/article/details/81281117 上周完成一个PC端的项目,对于我这样的小白来说,这个项目里面最大的问题 ...

  5. JavaScript判断移动端及pc端访问不同的网站

    JavaScript判断移动端及pc端访问不同的网站 现在很多网站都是分为两个版本,一个pc端的一个移动端的(响应式除外),针对这两个版本,就需要对访问的设备进行判断,如果是pc,就直接访问pc网站, ...

  6. 自动识别移动端还是PC端

    平时在开发中经常会遇到这样的需求,除了开发PC端之外,还会同时开发移动端.对于简单的页面,可以使用bootstrap之类的框架实现响应式页面,可是当页面很复杂的时候,就需要开发一个移动端页面,一个PC ...

  7. CoCos2dx开发:PC端调试运行正常但打包apk文件后在手机上点击闪退

    记:今天调试时出现的一个PC端调试运行正常,但打包apk文件后在手机上点击闪退的问题. 可能在不同的情况条件下,会有不同的原因导致apk安装后闪退问题.拿android studio等软件来说,开发安 ...

  8. 自适应PC端网页制作使用REM

    做一个PC端的网页,设计图是1920X1080的. 要在常见屏上显示正常(比例正确可) 1280X720 1366X768 1440X900 1920X1080 使用了几种办法 1.内容在一屏内显示的 ...

  9. Unity输出PC端(Windows) 拖拽文件到app中

    需求:给策划们写一个PC端(Window)的Excel导表工具.本来用OpenFile打开FileExplorerDialog后让他们自己选择想要添加的Excel文件就行了,结果有个需求是希望能拖拽E ...

随机推荐

  1. UITableViewCell 分割线如何满屏

    在iOS7中,UITableViewCell左侧会有默认15像素的空白.设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉. 但是在iOS8中,设置setSepar ...

  2. C++基础--字符串倒序输出

    (一)用基本的数组实现 #include "stdafx.h" #include <stdio.h> #include <string.h> int mai ...

  3. jquery mobile 自定义图标

    Jquery Mobile框架包含了一组最常用的移动应用程序所需的图标,为了减少下载的大小,Jquery Mobile包含的是的白色的图标sprite图片,并自动在图标后添加一个半透明的黑圈以确保在任 ...

  4. oracle查询所有用户表的表名、主键名称、索引、外键等

    1.查找表的所有索引(包括索引名,类型,构成列): select t.*,i.index_type from user_ind_columns t,user_indexes i where t.ind ...

  5. haproxy学习——安装(一)

    安装包:haproxy-1.5.4.tar.gz (挺小的,大约1.3M) ①.首先要sz到本地虚拟机上(centos-6.5),tar zxvf haproxy-1.5.4.tar.gz,完成解压. ...

  6. github代码上传教程

    github 上传代码步骤 一.git以及Github Git是个正快速成长的版本控制系统,它由GitHub维护. 优势: 1.支持离线开发,离线Repository. 2.强大的分支功能,适合多个独 ...

  7. IEEP-网络实施-项目交付流程

    1.项目交付流程 1.1 定义 项目交付流程规定了对项目实施的管理和作业控制要求,保证了工程项目实施按照规定的程序进行 1.2 重要性 1.2.1提高客户满意度 1.2.2 提高工程效率,节约成本 1 ...

  8. python manage.py startapp app 时候报错No module named _sqlite3

    python manage.py startapp app 报错如下: File "manage.py", line 10, in <module>    execut ...

  9. postgres循环sql

    CREATE OR replace function loop_addDevice(i integer) RETURNS integer as $$ declare count alias ; all ...

  10. Catalan数列

    引入 今天听学长讲了卡特兰数列后对其有了更深的认识,在此完善了一下之前的博客加以总结. 首先用一个经典的例子来描述一下Catalan数列,我们有一个1~n的数列和一个大小为n的栈,我们有如下两种操作: ...