vue项目写密码框组件 仿京东(pc)

vue项目写密码框组件 仿京东(pc)

百度搜索好多有关pc端输入密码框的组件,没有找到心仪的,所以决定自己封装一个类似京东输入密码框的组件。

templete

<template>
    <div class='am-payPwd' :id="`ids_${id}`">
        <input type="password"
               readonly
               onfocus="this.removeAttribute('readonly');"
               maxlength="1"
               autocomplete="new-password"
               @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>

script

<script>
    export default {
        data () {
            return {
                pwdList: [],
                oldPwdList: [],
                isDelete: false,
                ipt: ''
            }
        },
        props: {
            id: {
                type: Number,
                default: 1          // 当一个页面有多个密码输入框时,用id来区分
            }
        },
        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 < 6) this.ipt[index].focus()
                }
            }
        }
    }
</script>

style

<style lang="less" rel="stylesheet/less">
    .am-payPwd {
        display: inline-block;
        border-radius: 5px;
        padding: 10px 0;
        border: 1px solid #cccccc;
        position: relative;
        margin-left: 1px;
        .shortInput {
            text-align: center;
            font-size: 30px;
            float: left;
            width: 40px;
            height: 20px !important;
            color: #333;
            outline: #ff0067;
            border-right: 1px solid #eee;
            &:last-child{
                border-right:0;
             }
        }
    }
</style>
这个组件不需要安装任何插件 是自己封装的一个 拿过去直接用就可以了

页面调用

1.引入组件

import paycodeApp from  '@/components/paycodeApp.vue'

2.组件注册

components:{
  paycodeApp
},

3.页面使用

<paycode-app></paycode-app>

附上效果图

以上就是封装的代码和如何使用的代码,如有什么问题欢迎留言。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 4,886评论 0 1
  • 前言 您将在本文当中了解到,往网页中添加数据,从传统的dom操作过渡到数据层操作,实现同一个目标,两种不同的方式....
    itclanCoder阅读 26,121评论 1 12
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    小姜先森o0O阅读 13,305评论 0 72
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    王喂马_阅读 11,526评论 1 77
  • 又到了每天晚上写日记的时间了。每天通过简书软件来写日记,我现在觉得它不单是在记录我每天所经历的事情,反而更像是我的...
    尹晨曦妈妈阅读 1,365评论 0 6