vue 解決文本框被鍵盤遮住的問題
更新時(shí)間:2019年11月06日 15:03:47 作者:echo_Ae
今天小編就為大家分享一篇vue 解決文本框被鍵盤遮住的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
如下所示:


我把它寫成了組件
主要代碼是
document.getElementById(this.FullScreenId).scrollTop = document.getElementById(this.FullScreenId).scrollHeight
我這邊把div滿屏了看下面css就知道了
你也可以使用body,這個(gè)你行百度一下就可以了
注意點(diǎn)是css
/* 頁(yè)面滿屏 */
.pageFullScreen {
height: 100%;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: auto;
}
父組件
<style>
</style>
<template>
<div id="testFullScreenId" class="pageFullScreen">
<div style="height: 500px; background-color: #eee;overflow-y: scroll;-webkit-overflow-scrolling: touch;"></div>
<inputList type="test" @inputListClick="inputListClick" FullScreenId="testFullScreenId" v-model="inputVal" @inputListFocus="focus" @inputListBlur="blur"></inputList>
<br />
<button @click="sub">獲取input值</button>
<br />
<inputList type="test" @inputListClick="inputListClick" FullScreenId="testFullScreenId" v-model="inputVal" @inputListFocus="focus" @inputListBlur="blur"></inputList>
</div>
</template>
<script>
import inputList from '../../components/input' // input
export default {
components: {
inputList
},
data() {
return {
imagesUrl: { // 排版圖片使用懶加載
},
inputVal: ''
}
},
created() {
},
mounted() {
},
methods: {
focus(e) {
console.log(e)
},
blur(e) {
console.log(e)
},
inputListClick(e) {
console.log(e)
},
sub() {
alert(this.inputVal)
console.log(this.inputVal)
}
}
}
</script>
子組件
<style>
.inputList {}
</style>
<template>
<!-- 父組件使用v-model發(fā)送input事件就可以接收到了 -->
<div class="inputList">
<input :type="type" @click="inputListClick($event)" @input="inputListInput($event)" @focus="inputListFocus($event)" @blur="inputListBlur($event)">
</div>
</template>
<script>
export default {
name: 'inputList',
props: {
type: { // 文本框類型
type: String,
default: 'text'
},
FullScreenId: { // 文本框被鍵盤遮住
type: String,
default: ''
}
},
data() {
return {
imagesUrl: { // 排版圖片使用懶加載
},
pageHeightOne: '', // 頁(yè)面高度
pageHeightTow: ''
}
},
created() {
},
mounted() {
// window.onresize監(jiān)聽頁(yè)面高度的變化
window.onresize = () => {
return (() => {
// window.scroll(0, 0) // 滾到頂部
document.getElementById(this.FullScreenId).scrollTop = document.getElementById(this.FullScreenId).scrollHeight
})()
}
},
methods: {
// 鍵盤輸入
inputListInput(e) {
if (typeof e.target.value === 'string') {
if (/[\u4e00-\u9fa5]/.test(e.target.value)) {
e.target.value = ''
} else {
// console.log('合格')
}
} else {
console.log('數(shù)據(jù)類型不正確')
}
this.$emit('input', e.target.value)
},
// 獲取焦點(diǎn)
inputListFocus(e) {
// console.log(e)
this.$emit('inputListFocus', 'focus')
},
// 失去焦點(diǎn)
inputListBlur(e) {
// console.log(e)
this.$emit('inputListBlur', 'blur')
},
// 點(diǎn)擊
inputListClick(e) {
this.$emit('inputListClick', 'click')
}
}
}
</script>
相關(guān)文章
Vite3結(jié)合Svelte3使用@import導(dǎo)入scss樣式
這篇文章主要為大家介紹了Vite3結(jié)合Svelte3使用@import導(dǎo)入scss樣式實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
vue2.0 如何在hash模式下實(shí)現(xiàn)微信分享
這篇文章主要介紹了vue2.0 如何在hash模式下實(shí)現(xiàn)微信分享,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
Ant?Design?Vue?Pagination分頁(yè)組件的封裝與使用
這篇文章主要介紹了Ant?Design?Vue?Pagination分頁(yè)組件的封裝與使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vue動(dòng)態(tài)注冊(cè)組件實(shí)例代碼詳解
寫本篇文章之前其實(shí)也關(guān)注過vue中的一個(gè)關(guān)于加載動(dòng)態(tài)組件is的API,最開始研究它只是用來實(shí)現(xiàn)一個(gè)tab切換的功能,需要的朋友可以參考下2019-05-05

