問題:聊天頁面,如果輸入框設置了adjust-position屬性為true,會導致鍵盤彈起時,整個頁面上移,頂部導航欄也會跟著上移。
我想要的效果:鍵盤彈起時,頁面內容上移,頂部導航欄保持不動
解決方法:
1、先將 :adjust-position="false"
2、使用 uni.onKeyboardHeightChange 方法監聽鍵盤高度變化
data() {return {keyboardHeight: 0,}
},
onReady() {// 監聽鍵盤高度變化uni.onKeyboardHeightChange((res) => {if (res.height > 0) {this.keyboardHeight = res.height;} else {this.keyboardHeight = 0;}});
},
ps:也可以用textarea里的方法@keyboardheightchange="keyboardheightchange"去監聽鍵盤高度
3、頁面內容設置css:
<view class="box-2" :style="[{transform:`translateY(-${keyboardHeight}px)`}]">
</view>