vue实现密码显示与隐藏的最简方法

vue实现密码显示与隐藏的最简方法


更新踩坑:

在element-ui中:

  • 2.15.17版本以前,type=“password”类型的input在输入内容后会自动显示小眼睛
  • 在目前最新版本2.15.17中,需要加入 show-password 才可以
    在这里插入图片描述
    ------------分割线--------------------------------------------------------------------

若需手动实现密码显示与隐藏,可接着往下看(自动忽略我用的elementui,那时候我还不知道它自带呜呜)
实现效果:(画质略糊见谅)
在这里插入图片描述
要求:闭眼icon覆盖睁眼icon看起来和其本身一样
我用的 icon链接:http://www.fontawesome.com.cn/faicons/
完整代码:(有点长,但涉及密码显示隐藏的内容很短)

<template>
    <div class="login">
        <div class="loginTitle">
            管理员登录
        </div>
        <div class="rowEditor">
            <el-input v-model="userCode" placeholder="请输入工号"></el-input>
            <i class="fa fa-user-o fa-lg"></i>
        </div>
        <div class="rowEditor">
            <el-input v-model="userPwd" :type="psw?'password':'text'" placeholder="请输入密码"></el-input>
            <i class="fa fa-lock fa-lg"></i>
            <!-- 因为闭眼睁眼的icon重叠,闭眼icon在先,睁眼icon在后,才能触发到睁眼icon上的click事件 -->
            <i class="fa fa-eye-slash fa-lg" v-if="!psw" aria-hidden="true"></i>
            <i class="fa fa-eye fa-lg" aria-hidden="true" @click="psw=!psw"></i>
        </div>
        <div class="submit">
            <el-button  type="primary" @click="checkLogin">登录系统</el-button>
            <el-button type = "text">忘记密码</el-button>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      psw: true,
      userCode: '',
      userPwd: ''
    }
  }
}
</script>

<style lang="less" scoped>  // 核心:让闭眼睁眼的icon重叠,其他都不重要
    .login{
        background-color: #fff;
        box-shadow: 0 0 10px #999;
        width: 320px;
        padding: 20px;
        position: fixed;
        left: 50%;
        top:50%;
        transform: translate(-50%, -50%);
        border-radius: 16px;
        .loginImg{
            padding: 40px 0px;

            img{
                height: 100px;
                width: 100px;
                border-radius: 50%;
            }
        }
        .loginTitle{
            width: 120px;
            display: flex;
            justify-content: center;
            font-weight: bold;
            font-size: 1.5em;
            margin:0px 30% 15px;
            border-bottom: 3px solid #3A62D7;
        }

        .rowEditor{
            position: relative;
            margin-bottom: 10px;
            .fa-user-o,.fa-lock{
                position: absolute;
                left: 15px;
                top: 13px;
            }
            .fa-eye,.fa-eye-slash{  
                position: absolute;
                right: 15px;
                top: 13px;
            }
            ::v-deep .el-input__inner{
                padding-left: 36px;
                border-top-style: none;
                border-left-style: none;
                border-right-style: none;
                border-bottom: 2px solid #BBBBBB;
                border-radius: 0px;
            }
        }
        .submit{
            padding: 25px 0;
            display: flex;
            justify-content: space-around;
        }
    }
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值