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


我把它寫成了組件
主要代碼是
document.getElementById(this.FullScreenId).scrollTop = document.getElementById(this.FullScreenId).scrollHeight
我這邊把div滿屏了看下面css就知道了
你也可以使用body,這個你行百度一下就可以了
注意點是css
/* 頁面滿屏 */
.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: '', // 頁面高度
pageHeightTow: ''
}
},
created() {
},
mounted() {
// window.onresize監(jiān)聽頁面高度的變化
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)
},
// 獲取焦點
inputListFocus(e) {
// console.log(e)
this.$emit('inputListFocus', 'focus')
},
// 失去焦點
inputListBlur(e) {
// console.log(e)
this.$emit('inputListBlur', 'blur')
},
// 點擊
inputListClick(e) {
this.$emit('inputListClick', 'click')
}
}
}
</script>
相關(guān)文章
Vue響應(yīng)式原理與虛擬DOM實現(xiàn)步驟詳細講解
在Vue中最重要、最核心的概念之一就是響應(yīng)式系統(tǒng)。這個系統(tǒng)使得Vue能夠自動追蹤數(shù)據(jù)變化,并在數(shù)據(jù)發(fā)生變化時自動更新相關(guān)的DOM元素。本文將會探討Vue響應(yīng)式系統(tǒng)的實現(xiàn)原理及其底層實現(xiàn)2023-03-03
Vue axios與Go Frame后端框架的Options請求跨域問題詳解
這篇文章主要介紹了Vue axios與Go Frame后端框架的Options請求跨域問題詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
vue cli如何配置開發(fā)環(huán)境下的sourcemap
這篇文章主要介紹了vue cli如何配置開發(fā)環(huán)境下的sourcemap問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
vue+vue-meta-info動態(tài)設(shè)置meta標(biāo)簽教程
這篇文章主要介紹了vue+vue-meta-info動態(tài)設(shè)置meta標(biāo)簽教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
解決vue數(shù)據(jù)更新但table內(nèi)容不更新的問題
這篇文章主要給大家介紹了vue數(shù)據(jù)更新table內(nèi)容不更新解決方法,文中有詳細的代碼示例供大家作為參考,感興趣的同學(xué)可以參考閱讀一下2023-08-08

