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

vue实现点击按钮input保持聚焦状态

主要功能:

  1. 点击"停顿"按钮切换对话框显示状态
  2. 输入框聚焦时保持状态
  3. 点击对话框外的区域自动关闭
    在这里插入图片描述

以下是代码版本:

<template><div class="input-container"><el-inputv-model="input"style="width: 240px"placeholder="Please input"ref="inputRef"class="input-ref"@focus="handleFocus"/><el-button class="input-btn" @click.stop="toggleDialog" :disabled="!isFocused":type="showDialog ? 'primary' : ''">停顿 {{ isFocused ? 'ON' : 'OFF' }}</el-button><transition name="fade"><div v-if="showDialog" class="dialog-wrapper" @click.stop><dl class="dialog-content"><dt>插入内容</dt><dd @click="closeDialog" style="cursor: pointer">插入btn</dd></dl></div></transition></div>
</template><script lang="ts" setup>
import { ref, onMounted, onBeforeUnmount } from "vue";const showDialog = ref(false);
const input = ref("");
const inputRef = ref<HTMLInputElement>();
const isFocused = ref(false);const handleDocumentClick = (e: MouseEvent) => {const target = e.target as HTMLElement;const clickedInside = target.closest(".input-container");if (!clickedInside && isFocused.value) {closeDialog();}
};onMounted(() => {document.addEventListener("click", handleDocumentClick);
});onBeforeUnmount(() => {document.removeEventListener("click", handleDocumentClick);
});function closeDialog() {showDialog.value = false;if (inputRef.value) {inputRef.value.blur();}isFocused.value = false;
}function handleFocus() {isFocused.value = true;
}function toggleDialog() {showDialog.value = !showDialog.value;if (inputRef.value) {inputRef.value.focus();}
}
</script><style scoped>
.input-container {position: relative;display: inline-block;
}.dialog-wrapper {position: absolute;top: 100%;left: 0;margin-top: 8px;padding: 12px;background: white;border: 1px solid #ebeef5;border-radius: 4px;box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);z-index: 2000;
}.fade-enter-active, .fade-leave-active {transition: opacity 0.2s;
}
.fade-enter-from, .fade-leave-to {opacity: 0;
}
</style>

主要优点:

  1. 更好的结构

    • 添加了容器元素 .input-container 方便定位
    • 使用 transition 实现平滑的对话框动画
  2. 安全的实现

    • 添加了类型定义 inputRef.valueHTMLInputElement
    • 使用生命周期钩子管理事件监听
    • 添加了点击对话框防止事件冒泡的 @click.stop
  3. 用户体验

    • 按钮状态更直观 (添加了 primary 样式)
    • 添加了过渡动画
    • 对话框样式更美观
  4. 代码组织

    • 重命名方法更语义化 (toggleDialog 替代 [pauseInput]
    • 分离了关闭逻辑到 closeDialog 方法
  5. DOM 检查优化

    • 使用整个容器检查替代具体元素检查
http://www.lryc.cn/news/2401804.html

相关文章:

  • [蓝桥杯]取球博弈
  • Spring Security入门:创建第一个安全REST端点项目
  • [Java 基础]数组
  • fastadmin fildList 动态下拉框默认选中
  • java学习笔记——数组和二维数组
  • ‘pnpm‘ 不是内部或外部命令,也不是可运行的程序
  • Android Test2 获取系统android id
  • webpack打包学习
  • 基于Java(Jsp+servelet+Javabean)+MySQL实现图书管理系统
  • 服务器CPU被WMI Provider Host系统进程占用过高,导致系统偶尔卡顿的排查处理方案
  • JavaSwing之--JMenuBar
  • vue3+elementplus表格表头加图标及文字提示
  • 【物联网-S7Comm协议】
  • NLP中的input_ids是什么?
  • LeetCode Hot100刷题——划分字母区间
  • c++ 基于OpenSSL的EVP接口进行SHA3-512和SM3哈希计算
  • Vue3实现拖拽改变元素大小
  • Spring IoC 详解:原理、实现与实战
  • 深入Java NIO:构建高性能网络应用
  • 数据分析后台设计指南:实战案例解析与5大设计要点总结
  • 深度学习之模型压缩三驾马车:基于ResNet18的模型剪枝实战(1)
  • SSH/RDP无法远程连接?腾讯云CVM及通用服务器连接失败原因与超全排查指南
  • 网络测试实战:金融数据传输的生死时速
  • 数据库系统概论(十四)详细讲解SQL中空值的处理
  • 【信创-k8s】海光/兆芯+银河麒麟V10离线部署k8s1.31.8+kubesphere4.1.3
  • [蓝桥杯]三体攻击
  • 深入解析支撑向量机(SVM):原理、推导与实现
  • 一台电脑联网如何共享另一台电脑?网线方式
  • 面试题:SQL 中如何将 多行合并为一行(合并行数据为列)?
  • MacroDroid安卓版:自动化操作,让生活更智能