Vue Components 數(shù)字鍵盤的實(shí)現(xiàn)
本文介紹了Vue Components 數(shù)字鍵盤的實(shí)現(xiàn),分享給大家,具體如下:
項(xiàng)目地址 點(diǎn)擊查看
項(xiàng)目演示 點(diǎn)擊查看
首先監(jiān)聽(tīng)所有的input,有input聚焦時(shí)調(diào)起數(shù)字鍵盤,通過(guò)getBoundingClientRect判斷input位置讓數(shù)字鍵盤在input附近,失去焦點(diǎn)后則隱藏?cái)?shù)字鍵盤
let inputElement = document.getElementsByTagName("input");
[...inputElement].forEach(ipele => {
ipele.addEventListener("focus", function(e) {
$this.inputTarget = e.target;
let scrollTop =
window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop;
let scrollLeft =
window.pageXOffset ||
document.documentElement.scrollLeft ||
document.body.scrollLeft;
$this.NumberkeyBoardStyle =
"top:" +
(e.target.getBoundingClientRect().top +
scrollTop +
e.target.offsetHeight) +
"px;left:" +
(e.target.getBoundingClientRect().left +
scrollLeft +
e.target.offsetWidth) +
"px";
});
ipele.addEventListener("blur", function(e) {
$this.inputTarget = null;
$this.NumberkeyBoardStyle = "display:none";
});
});
點(diǎn)擊小鍵盤時(shí)阻止默認(rèn)事件,阻止input失去焦點(diǎn)。
BoardNumberKeyDown(e) {
if (e && e.preventDefault) {
e.preventDefault();
} else {
window.event.returnValue = false;
return false;
}
},
點(diǎn)擊小鍵盤時(shí),根據(jù)事件委托,得出點(diǎn)擊的數(shù)字,然后根據(jù)selectionStart,selectionEnd獲取input中的焦點(diǎn),將小鍵盤中的數(shù)字插入焦點(diǎn)處,最后焦點(diǎn)右移一位。
let inputTarget = this.inputTarget;
let Pointstart = inputTarget.selectionStart;
let PointEnd = inputTarget.selectionEnd;
let wordLength = inputTarget.value.length;
if (e.target.className == "numberTD" && e.target.innerText != "←") {
inputTarget.value =
inputTarget.value.slice(0, Pointstart) +
e.target.innerText +
inputTarget.value.slice(PointEnd, wordLength);
//一個(gè)小數(shù)點(diǎn)和開(kāi)頭不能有小數(shù)點(diǎn)
inputTarget.value = inputTarget.value.replace(/^\./g, "");
inputTarget.value = inputTarget.value
.replace(".", "$#$")
.replace(/\./g, "")
.replace("$#$", ".");
inputTarget.selectionStart = Pointstart + 1;
inputTarget.selectionEnd = Pointstart + 1;
//讓光標(biāo)顯示在input可視區(qū)域
inputTarget.blur();
inputTarget.focus();
點(diǎn)擊刪除鍵時(shí),刪除光標(biāo)處數(shù)字,最后光標(biāo)右移。
if (e.target.className == "numberTD" &&
e.target.innerText == "←" &&
//PointEnd==0時(shí)會(huì)復(fù)制整個(gè)input的value
PointEnd != 0
) {
inputTarget.value =
inputTarget.value.slice(0, Pointstart - 1) +
inputTarget.value.slice(PointEnd, wordLength);
inputTarget.selectionStart = Pointstart - 1;
inputTarget.selectionEnd = Pointstart - 1;
//讓光標(biāo)顯示在input可視區(qū)域
inputTarget.blur();
inputTarget.focus();
}
在刪除數(shù)字和添加數(shù)字后要讓input失去焦點(diǎn)在獲取焦點(diǎn),不然光標(biāo)會(huì)在最后而不是在input的可視區(qū)域,這樣子會(huì)看不到輸入的值,具體可以查看項(xiàng)目。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中使用console.log打印的實(shí)現(xiàn)
這篇文章主要介紹了vue中使用console.log打印的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue3中使用pinia(大菠蘿)狀態(tài)管理倉(cāng)庫(kù)的項(xiàng)目實(shí)踐
本文主要介紹了vue3中使用pinia(大菠蘿)狀態(tài)管理倉(cāng)庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
springboot?vue接口測(cè)試前端動(dòng)態(tài)增刪表單功能實(shí)現(xiàn)
這篇文章主要為大家介紹了springboot?vue接口測(cè)試前端動(dòng)態(tài)增刪表單功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Vue?+?SpringBoot?實(shí)現(xiàn)文件的斷點(diǎn)上傳、秒傳存儲(chǔ)到Minio的操作方法
這篇文章主要介紹了Vue?+?SpringBoot?實(shí)現(xiàn)文件的斷點(diǎn)上傳、秒傳存儲(chǔ)到Minio的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-06-06
vue之a(chǎn)-table中實(shí)現(xiàn)清空選中的數(shù)據(jù)
今天小編就為大家分享一篇vue之a(chǎn)-table中實(shí)現(xiàn)清空選中的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue?echarts實(shí)現(xiàn)改變canvas長(zhǎng)和寬自適應(yīng)
這篇文章主要介紹了vue?echarts實(shí)現(xiàn)改變canvas長(zhǎng)和寬自適應(yīng)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
圖文講解用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟
本次小編給大家?guī)?lái)的是關(guān)于用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2019-02-02
Vue input輸入框中的值如何變成黑點(diǎn)問(wèn)題
這篇文章主要介紹了Vue input輸入框中的值如何變成黑點(diǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue監(jiān)聽(tīng)某個(gè)元素以外的區(qū)域被點(diǎn)擊問(wèn)題
這篇文章主要介紹了Vue監(jiān)聽(tīng)某個(gè)元素以外的區(qū)域被點(diǎn)擊問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

