【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 这个变量的变化,即可实现。

相关推荐
脾气有点小暴4 小时前
scroll-view分页加载
前端·javascript·uni-app
脾气有点小暴6 小时前
uniapp自定义头部导航
前端·uni-app
一 乐8 小时前
健身房预约|基于springboot + vue健身房预约小程序系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·小程序
前端 贾公子11 小时前
[uniapp][swtich开关]阻止切换状态(类似阻止事件冒泡)
uni-app
豌豆学姐12 小时前
Sora2 能做什么?25 秒视频生成 API 的一次接入实践
大数据·人工智能·小程序·aigc·php·开源软件
李慕婉学姐12 小时前
【开题答辩过程】以《智慧校园创新互助小程序的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot·小程序
qq_124987075314 小时前
基于微信小程序的校园跑腿系统的设计与实现(源码+论文+部署+安装)
spring boot·微信小程序·小程序·毕业设计·计算机毕业设计
雪芽蓝域zzs15 小时前
uniapp基于picker选择器实现年月日时分秒
uni-app
niucloud-admin15 小时前
本地开发部署——uniapp端站点部署
uni-app
小小王app小程序开发16 小时前
盲盒小程序一番赏特殊玩法超细分拆解:从底层逻辑到落地细节
大数据·小程序