vue實(shí)現(xiàn)點(diǎn)擊按鈕input保持聚焦?fàn)顟B(tài)的示例代碼
主要功能:
- 點(diǎn)擊"停頓"按鈕切換對話框顯示狀態(tài)
- 輸入框聚焦時保持狀態(tài)
- 點(diǎn)擊對話框外的區(qū)域自動關(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)平滑的對話框動畫
安全的實(shí)現(xiàn):
- 添加了類型定義
inputRef.value為HTMLInputElement - 使用生命周期鉤子管理事件監(jiān)聽
- 添加了點(diǎn)擊對話框防止事件冒泡的
@click.stop
用戶體驗(yàn):
- 按鈕狀態(tài)更直觀 (添加了 primary 樣式)
- 添加了過渡動畫
- 對話框樣式更美觀
代碼組織:
- 重命名方法更語義化 (
toggleDialog替代 [pauseInput] - 分離了關(guān)閉邏輯到
closeDialog方法
DOM 檢查優(yōu)化:
- 使用整個容器檢查替代具體元素檢查
到此這篇關(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue監(jiān)聽使用方法和過濾器實(shí)現(xiàn)
這篇文章主要介紹了Vue監(jiān)聽使用方法和過濾器實(shí)現(xiàn),過濾器為頁面中數(shù)據(jù)進(jìn)行強(qiáng)化,具有局部過濾器和全局過濾器2022-06-06
Xx-vue自定義指令實(shí)現(xiàn)點(diǎn)擊水波紋漣漪效果
這篇文章主要為大家介紹了Xx-vue自定義指令實(shí)現(xiàn)點(diǎn)擊水波紋漣漪效果,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
vue項(xiàng)目啟動后沒有局域網(wǎng)地址問題
這篇文章主要介紹了vue項(xiàng)目啟動后沒有局域網(wǎng)地址問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
解決Element組件的坑:抽屜drawer和彈窗dialog
這篇文章主要介紹了解決Element組件的坑:抽屜drawer和彈窗dialog問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
antd upload上傳組件如何獲取服務(wù)端返回數(shù)據(jù)
這篇文章主要介紹了antd upload上傳組件如何獲取服務(wù)端返回數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
vue定時器清除不掉,導(dǎo)致功能頻繁執(zhí)行問題
這篇文章主要介紹了vue定時器清除不掉,導(dǎo)致功能頻繁執(zhí)行問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

