当前位置: 首页 > ds >正文

Uniapp之键盘弹窗

适用范围

  1. 评论输入框
  2. 自定义键盘顶部工具栏
  3. 其他吸附于键盘顶部的弹框

意义

对于输入框来说,一般都包含自动抬高输入框,但是对于一些自定义窗口来说(比如输入框下添加工具栏、键盘顶部添加工具栏等),就需要自己处理弹框位置了,那么这个时候键盘弹窗就解决了复杂的处理。

示例

下面示例用于展示 keyboard-popup 使用示例,当 textarea 聚焦时,textarea 就会自动抬高到键盘上方。

<template><!-- 评论输入框 --><!-- 注意这里需要添加adjustPosition=false属性来关闭自动上推页面 --><keyboard-popup ref="replyPopup"><textarearef="textarea"v-model="content"placeholder="请输入内容"focusauto-focusmaxlength="500":style="{height: '160rpx'}":adjustPosition="false":showConfirmBar="false"></textarea></keyboard-popup>
</template>
<script>
export default {data() {return {content: ''}},methods: {openPopup() {// 打开弹窗this.$refs.replyPopup.open()},openPopup() {// 关闭弹窗this.$refs.replyPopup.colse()}},mounted() {// 监听键盘高度变化,用于抬高评论框this.$refs.replyPopup.handleKeyboardHeightChange()},onUnload() {// 取消键盘高度变化监听this.$refs.replyPopup.offKeyboardHeightChange()},
}
</script>

源码

以下是 KeyboardPopup 组件的源码,可根据自己的需求加以改进。

<script>
export default {name: 'KeyboardPopup',props: {// 是否显示底部安全距离showSafeBottom: {type: Boolean,default: true}},data() {return {showVal: false,keyboardHeight: 0,isOpen: false,safeAreaInsetBottom: 0}},methods: {open() {this.showVal = truethis.$nextTick(() => {this.isOpen = true})},close() {this.isOpen = falsethis.showVal = false},onClose() {this.isOpen = falsethis.showVal = falsethis.$emit('close')},/*** 设置键盘高度,用于设置内容抬高的位置* @param height 高度*/setKeyboardHeight(height) {this.keyboardHeight = height},handleKeyboardHeightChange() {uni.onKeyboardHeightChange(res => {this.keyboardHeight = res.height})},offKeyboardHeightChange() {uni.offKeyboardHeightChange()}},mounted() {// 获取底部安全距离if (this.showSafeBottom) {this.safeAreaInsetBottom = uni.getSystemInfoSync().safeAreaInsets.bottom}}
}
</script><template><u-popup :show="showVal" @close="onClose" bgColor="transparent" :safeAreaInsetBottom="false" closeOnClickOverlay><view class="content-container" :style="{bottom: keyboardHeight + 'px'}"><slot :isOpen="isOpen"></slot></view><!--  底部安全距离  --><view class="safeBottom" v-if="!isOpen" :style="{height: safeAreaInsetBottom + 'px'}"></view></u-popup>
</template><style scoped lang="scss">
.content-container {position: relative;
}.safeBottom {width: 100%;background-color: #fff;
}
</style>
http://www.xdnf.cn/news/15906.html

相关文章:

  • win10连接鼠标自动关闭触摸板/win10关闭触摸板(笔记本)
  • 智能合约代理与批量调用优化:最小代理与MultiCall的应用
  • android studio libs.versions.toml 配置
  • 嵌入式硬件中电感的基本原理与实现详解
  • CSS篇——第二章 六十五项关键技能(下篇)
  • Kotlin方差
  • OpenCV 官翻5 - 机器学习
  • 智能制造——解读39页汽车行业数字化工厂解决方案【附全文阅读】
  • 考研408《计算机组成原理》复习笔记,第三章(5)——磁盘存储器
  • 文生图-StoryGAN:用于故事可视化的顺序条件GAN
  • Github Actions Workflows 上传 Dropbox
  • 【C++】初识C++(2)
  • 【RK3576】【Android14】UART开发调试
  • 微信小程序入门实例_____从零开始 开发一个“旅行清单 ”微信小程序
  • 微信小程序——世界天气小助手
  • 【EMC设计基础--信号环路分析、PCB设计规则】
  • VSCode - VSCode 查找中文字符
  • LVS工作模式和算法的总结
  • 【RK3576】【Android14】SDK源码编译
  • 前端 SSE 实战应用:用最简单的方式实现实时推送
  • Android CountDownTimer
  • 深入理解Linux文件I/O:系统调用与标志位应用
  • 机器学习17-Mamba
  • c++继承详解
  • 【Leecode 随笔】
  • 使用python的读取xml文件,简单的处理成元组数组
  • 【时时三省】(C语言基础)通过指针引用字符串
  • PyCharm 高效入门指南(核心模块详解二)
  • stm32f4 dma的一些问题
  • API和SDK有何区别??