element-ui el-input 密码输入框自定义显示隐藏图标

46 篇文章 1 订阅
16 篇文章 1 订阅

准备图标或者图片

 

 <el-input>标签代码

<el-input ref="pwdInput"
          :type="pwdObj.pwdType"
          v-model="password"
          @focus="focusEnd($event)">
  <!-- input中加图标必须要有slot="suffix"属性,不然无法显示图标 -->
  <el-image slot="suffix"
            class="img-sty"
            :src="getIconUrl(pwdObj.pwdType === 'text' ? 'offeye' : 'openeye')"
            fit="scale-down"
            @click="changeye('pwdType', 'pwdInput')" />
</el-input>

 <script type="text/javascript">中代码

<script>
  export default {
	data() {
		return {
            password: '',
			pwdObj: { pwdType: 'password'}
		}
	},
	computed: {
		// 获取图标
		getIconUrl() {
			return function (name, type = 'svg') {
				return require(`@/assets/img/icons/${name}.${type}`)
			}
		},
	methods: {
		//点击图标控制密码的显示和隐藏
		changeye(typeName, refName) {
			this.$set(
				this.pwdObj,
				`${typeName}`,
				this.pwdObj[`${typeName}`] === 'password' ? 'text' : 'password'
			)
			this.$refs[`${refName}`].focus()
		},
		//点击查看密码图标使光标定位到最后
		focusEnd(e) {
			//input获取光标显示在最后
			const obj = e.srcElement,
				// obj.focus()
				len = obj.value.length
			//光标定位要加上 setTimeOut,不然就会重新光标定位失败
			setTimeout(() => {
				obj.selectionStart = obj.selectionEnd = len
			}, 60)
		}
	}
}
</script>

 可能自定义图标后,显示的位置没那么准确,根据需要通过css调整

.img-sty {
  cursor: pointer;
  margin-top: 10px;
}

 最终效果

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值