vue實(shí)現(xiàn)點(diǎn)擊按鈕input保持聚焦?fàn)顟B(tài)的示例代碼
主要功能:
- 點(diǎn)擊"停頓"按鈕切換對(duì)話框顯示狀態(tài)
- 輸入框聚焦時(shí)保持狀態(tài)
- 點(diǎn)擊對(duì)話框外的區(qū)域自動(dòng)關(guān)閉

以下是代碼版本:
<template>
<div class="input-container">
<el-input
v-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>插入內(nèi)容</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>主要優(yōu)點(diǎn):
更好的結(jié)構(gòu):
- 添加了容器元素
.input-container方便定位 - 使用 transition 實(shí)現(xiàn)平滑的對(duì)話框動(dòng)畫
安全的實(shí)現(xiàn):
- 添加了類型定義
inputRef.value為HTMLInputElement - 使用生命周期鉤子管理事件監(jiān)聽
- 添加了點(diǎn)擊對(duì)話框防止事件冒泡的
@click.stop
用戶體驗(yàn):
- 按鈕狀態(tài)更直觀 (添加了 primary 樣式)
- 添加了過渡動(dòng)畫
- 對(duì)話框樣式更美觀
代碼組織:
- 重命名方法更語義化 (
toggleDialog替代 [pauseInput] - 分離了關(guān)閉邏輯到
closeDialog方法
DOM 檢查優(yōu)化:
- 使用整個(gè)容器檢查替代具體元素檢查
到此這篇關(guān)于vue實(shí)現(xiàn)點(diǎn)擊按鈕input保持聚焦?fàn)顟B(tài)的文章就介紹到這了,更多相關(guān)vue實(shí)現(xiàn)點(diǎn)擊按鈕input保持聚焦?fàn)顟B(tài)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 修改 data 數(shù)據(jù)問題并實(shí)時(shí)顯示操作
這篇文章主要介紹了vue 修改 data 數(shù)據(jù)問題并實(shí)時(shí)顯示操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
antd vue表格可編輯單元格以及求和實(shí)現(xiàn)方式
這篇文章主要介紹了antd vue表格可編輯單元格以及求和實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue源碼解析之?dāng)?shù)據(jù)響應(yīng)系統(tǒng)的使用
這篇文章主要介紹了Vue源碼解析之?dāng)?shù)據(jù)響應(yīng)系統(tǒng)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue封裝實(shí)現(xiàn)自動(dòng)循環(huán)滾動(dòng)的列表
在做數(shù)據(jù)大屏開發(fā)的過程中,經(jīng)常出現(xiàn)需要對(duì)列表進(jìn)行自動(dòng)滾動(dòng)的需求,所以本文就來為大家介紹一下如何利用vue封裝一個(gè)自動(dòng)循環(huán)滾動(dòng)的列表吧2023-09-09
vue中keep-alive組件實(shí)現(xiàn)多級(jí)嵌套路由的緩存
本文主要介紹了vue中keep-alive組件實(shí)現(xiàn)多級(jí)嵌套路由的緩存,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
深入了解Vue3中偵聽器watcher的實(shí)現(xiàn)原理
在平時(shí)的開發(fā)工作中,我們經(jīng)常使用偵聽器幫助我們?nèi)ビ^察某個(gè)數(shù)據(jù)的變化然后去執(zhí)行一段邏輯。在?Vue.js?2.x?中,你可以通過?watch?選項(xiàng)去初始化一個(gè)偵聽器,稱作?watcher。本文將詳細(xì)為大家介紹一下偵聽器的實(shí)現(xiàn)原理,需要的可以參考一下2022-04-04
vue使用keep-alive保持滾動(dòng)條位置的實(shí)現(xiàn)方法
這篇文章主要介紹了vue使用keep-alive保持滾動(dòng)條位置的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

