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)文章
Vite3結(jié)合Svelte3使用@import導(dǎo)入scss樣式
這篇文章主要為大家介紹了Vite3結(jié)合Svelte3使用@import導(dǎo)入scss樣式實現(xiàn)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Ant?Design?Vue?Pagination分頁組件的封裝與使用
這篇文章主要介紹了Ant?Design?Vue?Pagination分頁組件的封裝與使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04