当前位置: 首页 > news >正文

编写一个指令(v-focus2end)使输入框文本在聚焦时焦点在文本最后一个位置

项目反馈输入框内容比较多时候,让鼠标光标在最后一个位置,心想什么奇葩需求,后面试了一下,是有点影响体验,于是就有了下面的效果,我目前的项目都是若依的架子,用的是vue2版本。vue3的朋友想要使用,自行调节

效果图如下:

在这里插入图片描述

使用方法:
  • 前提是指令被注册,代码不显示注册内容
<el-input v-focus2end v-model="dialog.form.actionSign" placeholder="请输入" clearable />
v-focus2end 指令
  • 还能继续优化,我就不想在操作了
/*** Copyright ©* # v-focus2end* @author: zw* @date: 2023-07-17*/export default {inserted(el) {const input = el instanceof HTMLInputElement ? el : el.querySelector('input')input.addEventListener('focus', focusEventListener.bind(input), false)input.addEventListener('blur', blurEventListener.bind(input), false)el.__focusEventListener = focusEventListenerel.__blurEventListener = blurEventListener},unbind(el) {const input = el instanceof HTMLInputElement ? el : el.querySelector('input')input.removeEventListener('focus', el.__focusEventListener, false)input.removeEventListener('blur', el.__blurEventListener, false)},
}function focusEventListener(e) {e.preventDefault()setTimeout(() => {const inputLength = this.value.lengththis.setSelectionRange(inputLength, inputLength)smoothMove.call(this)}, 300)
}function blurEventListener() {this.removeEventListener('focus', focusEventListener, false)
}function smoothMove() {const scrollMax = this.scrollWidth - this.clientWidthconst duration = 300const startTime = performance.now()function smoothScroll(timestamp) {const elapsedTime = timestamp - startTimeconst progress = Math.min(elapsedTime / duration, 1)const scrollPosition = progress * scrollMaxthis.scrollLeft = scrollPositionif (elapsedTime < duration) {requestAnimationFrame(smoothScroll.bind(this))}}requestAnimationFrame(smoothScroll.bind(this))
}
http://www.lryc.cn/news/118162.html

相关文章:

  • Virtualbox设置访问外网以及主机和虚拟机互通
  • 请简述React是什么?React的主要特点有哪些?React中有哪些主要组件?
  • DevOps最佳实践和工具在本地环境中的概述
  • kafka和rabbitmq之间的区别以及适用场景
  • python——案例15:判断奇数还是偶数
  • springboot汽车租赁后台java出租客户管理jsp源代码mysql
  • Linux学习之sed删除、追加、插入、更改、读写文件、下一行、打印、退出和seq命令
  • JuiceFS 在多云存储架构中的应用 | 深势科技分享
  • 什么是DNS的缓存?
  • smtplib.SMTPHeloError: (500, b‘Error: bad syntax‘)
  • /proc directory in linux
  • 装饰器模式(C++)
  • matlab使用教程(12)—随机数种子和随机数流
  • 【Java】JVM GC配置指南
  • 11.Eclipse 注释模板的说明及设置
  • Maven父子工程项目的配置
  • jumpserver命令记录膨胀问题
  • ControlNet
  • 竞赛项目 深度学习图像风格迁移 - opencv python
  • ​Windows dos文件夹与文件怎么创建与删除
  • JVM总结2
  • servlet三大类HttpSevlet,HttpServletRequest,HttpServletResponse介绍
  • 【雕爷学编程】Arduino动手做(12)---霍尔模块之霍尔磁感应声光报警器(磁控开关,接220V)
  • 认识 SQL
  • 【剑指Offer 58】翻转单词顺序,Java解密。
  • 微服务Ribbon-负载均衡原理
  • 如何实现Vue的异步组件?如何在Vue中使用本地存储?什么是Vue的指令模块化?
  • 《HeadFirst设计模式(第二版)》第六章代码——命令模式
  • JS 原型与继承2
  • 账号登录相关的一点随笔