微信小程序 textarea 层级过高问题简单解决方案

作者:microestc 时间:2024-04-18 09:36:41 

这篇文章主要介绍了微信小程序 textarea 层级过高问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

建立一个新的textarea 组件代替原生textarea ,废话不多说,上代码


<template>
 <view class="ui-textarea">
   <textarea class="textarea {{ hide? 'hidden':''}}" auto-height maxlength="{{maxlength}}" name="{{ name }}" value="{{ txt }}" placeholder="{{ placeholder }}" bindinput="onInput" bindblur="onBlur" focus="{{focus}}"></textarea>
   <view hidden="{{ hide == false }}" class="{{ txt === ''?'text placeholder':'text'}}" bindtap="onFocus">{{ txt ===''? placeholder:txt }}</view>
 </view>
</template>
<script>
export default {
 config: {
   usingComponents: {}
 },
 behaviors: [],
 properties: {
   placeholder: {
     type: String,
     value: ''
   },
   maxlength: {
     type: Number,
     value: 128
   },
   name: String,
   value: {
     type: String,
     value: ''
   }
 },
 data: {
   hide: true,
   txt: '',
   focus: false
 },
 ready() {
   if (this.data.value != '') {
     this.setData({ txt: this.data.value })
   }
 },
 methods: {
   onFocus() {
     this.setData({ hide: false, focus: true })
   },
   onInput(e) {
     this.setData({ txt: e.detail.value })
   },
   onBlur() {
     this.setData({ hide: true, focus: false })
   }
 }
}
</script>
<style lang="less">
.ui-textarea {
position: relative !important;

.textarea.hidden {
 display: block !important;
 position: absolute !important;
 left: -999px;
 right: -999px;
 top: 0;
}

.textarea {
 width: 100%;
 box-sizing: border-box;
}

.text {
 height: 100%;
 padding: 6px 5px;
 font-size: 14px;
}

.placeholder {
 color: #888;
}
}
</style>

Vue 代码,自己根据需求改一下 ,特别注意,不要用 wx;if 或者 hidden 属性 , wx:if 频繁渲染,虽然可以用,hidden 会出现 部分显示bug,[hidden] 会使auto-height 首次不正确

来源:https://www.cnblogs.com/microestc/p/11169191.html

标签:微信,小程序,textarea,层级
0
投稿

猜你喜欢

  • Django查询优化及ajax编码格式原理解析

    2021-04-26 02:04:27
  • sql server数据库最大Id冲突问题解决方法之一

    2012-01-05 19:28:42
  • 深入剖析Go语言编程中switch语句的使用

    2024-02-19 16:50:45
  • Python基于csv模块实现读取与写入csv数据的方法

    2023-04-12 23:14:34
  • python的多重继承的理解

    2021-04-27 16:54:21
  • Python Pyinstaller库安装步骤以及使用方法

    2021-05-12 07:07:26
  • python 实现数字字符串左侧补零的方法

    2021-07-07 10:34:43
  • 如何学习Python time模块

    2023-07-30 17:14:59
  • 国际上十四个优秀网页设计审核站

    2007-09-30 20:18:00
  • Python提取频域特征知识点浅析

    2021-10-31 08:01:31
  • python 如何读、写、解析CSV文件

    2022-09-21 13:59:33
  • PHP开发实现快递查询功能详解

    2023-11-24 12:19:39
  • 关于Python使用turtle库画任意图的问题

    2023-08-21 14:02:34
  • PHP 引用的概念

    2023-11-14 21:24:28
  • phpstorm断点调试方法图文详解

    2023-05-30 01:06:40
  • MySQL数据库查询之多表查询总结

    2024-01-13 21:49:47
  • Python中random模块生成随机数详解

    2023-06-13 16:11:52
  • 加快Firefox 3.5启动速度的方法

    2009-07-16 15:22:00
  • 如何在C++中调用Python

    2021-05-16 23:58:41
  • numpy.insert()的具体使用方法

    2021-12-23 15:33:34
  • asp之家 网络编程 m.aspxhome.com