【uniapp】关于小程序输入框聚焦、失焦(输入法占位)的问题

聊天小程序,界面带有输入框,当输入框中聚焦后,底部自动谈起输入法。此时输入框也要随之出现在输入法上方。默认情况下,输入框此时会被输入法覆盖掉。

以下是亲自实践,解决这个问题的方法:

一、小程序大概布局

复制代码
<template>
	<view style="height: 100%; display: flex; flex-direction: column;">
		
		<!-- 自定义导航栏 -->
		<head-nav :navRedirectPage="navRedirectPage"></head-nav>
		
		<!-- 小程序界面内容 -->
		<view class="container" :style="containerStyle">
			<!-- 聊天界面内容 -->
			<scroll-view ref='list' :scroll-top="scrollTop" class="chat-container" :scroll-y="true"></scroll-view>
			
			<!-- 底部输入框 -->
			<view class="bottom-bar"  :style="bottom_autosize_style">
				<view class="input-view">
						<uv-textarea selectable="true" :cursor-spacing="20" v-model="inputText" :inputBorder="false" customStyle="overflow: scroll;max-height: 75px;border-width: 0px !important;background-color: #F7F7FC;margin-left: 12px;padding: 7px;"
			:adjust-position="false" :show-confirm-bar='false' @blur="inputBindBlur" :maxlength="500" autoHeight placeholder="请输入内容" @focus="inputBindFocus"></uv-textarea>					
					<button class="send-button" @click="sendMessage"
						:disabled="!inputText.trim().length>0 || sendDisabled">
						<image class="send-message" src="/static/v2/aichat/fasong.png" />
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

css样式表:

  • 默认样式为:

    复制代码
      // 聊天窗口
      .chat-container {
      	flex-grow: 1; // 窗口大小为可变
      	overflow-y: auto;
      }
    
      // 底部输入框
      .bottom-bar {
      		position: relative;
      		display: flex;
      		align-items: center;
      		padding: 6px 13px 6px 13px;  // 主要是padding-bottom变化
      }
  • 当焦点在输入框内时,需要调整内容:

    // 聚焦方法
    inputBindFocus(e){
    console.log('e.detail.height:', e.detail.height)
    if (e.detail.height) {
    this.bottom_autosize_style = "padding-bottom:" + (e.detail.height + 6ssss) + "px;"
    if (!this.showScrollToBottom) {
    this.scrollToBottom();
    }
    }
    },
    // 失焦方法
    inputBindBlur(){
    // this.containerStyle = ' height:' + this.containerHeight + 'px;';
    this.bottom_autosize_style = ""
    if (!this.showScrollToBottom) {
    this.scrollToBottom();
    }
    }

请注意 bottom_autosize_style 这个变量的变化,即可实现。

相关推荐
说私域1 小时前
基于AI智能名片链动2+1模式服务预约商城系统的社群运营与顾客二次消费吸引策略研究
大数据·人工智能·小程序·开源·流量运营
StarChainTech6 小时前
无人机租赁平台:开启智能租赁新时代
大数据·人工智能·微信小程序·小程序·无人机·软件需求
计算机毕设指导66 小时前
基于微信小程序的运动场馆服务系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
码农客栈7 小时前
小程序学习(十一)之uni-app和原生小程序开发区别
学习·小程序·uni-app
说私域7 小时前
基于AI客服链动2+1模式商城小程序的社群运营策略研究——以千人社群活跃度提升为例
人工智能·微信·小程序·私域运营
mqjFKmYf8 小时前
VSC整流系统定功率控制系统的MATLAB仿真模型(支持MATLAB 2014a及以上版本)
小程序
Json____8 小时前
使用uni-app开发抖音小程序遇到previewImage方法图片加载不出来解决方案
小程序·uni-app
毕设源码-钟学长9 小时前
【开题答辩全过程】以 基于微信小程序教学评价平台的设计与实现为例,包含答辩的问题和答案
微信小程序·小程序
Jyywww1219 小时前
Uniapp+Vue3 使用父传子方法实现自定义tabBar
javascript·vue.js·uni-app
说私域9 小时前
基于AI智能名片链动2+1模式S2B2C商城小程序的流量运营策略研究
人工智能·微信·小程序·产品运营·流量运营