1.准备素材
打开网页:https://www.iconfont.cn/ 阿里巴巴矢量库

在这里插入图片描述
在这里插入图片描述
找到两个素材,下载即可

2.代码部分

<style>
    .box {
        position: relative;
        width: 400px;
        border-bottom: 1px solid #ccc;
        margin: 100px auto;
    }

    .box input {
        width: 370px;
        height: 30px;
        border: 0;
        outline: none;
    }

    .box img {
        position: absolute;
        top: 2px;
        right: 2px;
        width: 24px;
    }
</style>

<body>
    <div class="box">
        <input type="password" id="input">
        <img src="../img/close.png" id="eye">
    </div>

    <script>
        // 1.获取元素
        var input = document.getElementById('input');
        var eye = document.getElementById('eye');
        var flag = 0;
        // 2.注册事件 处理程序
        eye.onclick = function () {
        	// 应用flag 可以实现循环点击
            if (flag === 0) {
                // 点击一次后 flag 一定要变化
                input.type = 'password';
                // 修改 -图片路径
                eye.src = "../img/close.png";
                flag = 1;//赋值操作
            } else{
                input.type = 'text';
                // 修改 -图片路径
                eye.src = "../img/open.png";
                flag = 0;
            }

        }
    </script>

3.效果
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐