微信小程序vant 输入框问题处理方案

 更新时间:2023年09月08日 09:43:40   作者:浊清。。。  
这篇文章主要介绍了微信小程序vant输入框问题,本文给大家分享完美解决方案,结合实例代码给大家介绍的非常详细,需要的朋友可以参考下

在开发时候需要添加评论,点击的时候从底部弹起,效果如下图

开发过程中遇到的问题有如下几个:

1.van-field 搭配 van-popup
个别手机弹出后会导致输入框位置乱跳,问题原因是van-popup多次弹出数据渲染会有一定问题
2.van-field 搭配 van-overlay(遮罩)
遮罩弹出太慢,手机性能比较差的体验太差
3.IOS自动推上去内容跑掉

处理方案:

自义定遮罩,利用display进行设置,手机性能差的也几乎不会卡顿
参考的是网上一个小哥代码:https://www.jb51.net/javascript/2976631fc.htm

// wxml代码
// catchtouchmove="true" 对遮罩的穿透处理
<view class="overlayInput" style='display:{{showInput ? "block" : "none"}}' catchtouchmove="true" bindtap="onCancel"></view>
// wxss代码
// 这边代码是之前百度网上一个哥们的代码,
.overlayInput {
  display: none;
  position: fixed;
  top: 0%;
  left: 0%;
  width: 100%;
  height: 100%;
  background-color: black;
  z-index: 2;
  -moz-opacity: 0.7;
  opacity: 0.70;
  filter: alpha(opacity=70);
}

van-field代码如下,因为每次点击评论按钮需要自动吊起键盘,所以加一个wx:if,这样每次autofoucus都会生效,我这边是写成一个组件,父级调用的时候传值下来showInput

<view class="comInput" style="bottom: {{isBlur ? blurBottom : bottom}}px" wx:if="{{showInput}}">
  <view class="top hairline">
    <view class="cancel" bindtap="onCancel">取消</view>
    <view class="title">发表观点</view>
    <view class="release" bindtap="onRelease">发布</view>
  </view>
  <view class="comtent">{{title}}</view>
  <view style="padding:0 10px 10px 10px;">
    <van-field
      value="{{ inputValue }}"
      placeholder="内容经过官方合规性审查通过后对所有人可见"
      border="{{ false }}"
      cursor-spacing="95"
      input-class="border"
      bind:change="onChange"
      autosize="{{autoSize}}"
      bind:keyboardheightchange="keyboardheightchange"
      maxlength="200"
      adjust-position="{{false}}"
      arrow-direction="left"
      fixed="{{true}}"
      type="textarea"
      bind:focus="onFoucs"
      bind:blur="onBlur"
      bind:linechange="onLineChange"
      show-word-limit
      auto-focus
      focus
      placeholder-class="place"
      input-class="com-input-class"
      show-confirm-bar="{{ false }}"
    />
  </view>
</view>

优化处理:

由于小程序本身性能问题,每次键盘唤醒都可以看到很明显的卡顿,这边参考了微博小程序的做法,第一次弹起的时候记录一下位置,下次弹起直接跳转到对应位置

组件全部代码如下,有需要可以自行提取使用,需要自己修改部分:

// 样式
.overlay {
  display: none;
  position: absolute;
  top: 0%;
  left: 0%;
  width: 100%;
  height: 100%;
  background-color: black;
  z-index: 2;
  -moz-opacity: 0.7;
  opacity: 0.70;
  filter: alpha(opacity=70);
}
.comInput {
  /* height: 200px; */
  position: fixed;
  width: 100%;
  background: #fff;
  z-index: 20;
  /* bottom: 0; */
  border-radius: 26rpx 26rpx 0 0;
}
.top {
  padding: 10px 16px;
  text-align: center;
}
.cancel {
  float: left;
  width: 50px;
  text-align: left;
}
.title {
  display: inline-block;
}
.release {
  float: right;
  width: 100rpx;
  height: 50rpx;
  background:rgba(255,194,64,1);
  border-radius: 25rpx;
  font-size: 30rpx;
}
.hairline {
  border-bottom: 1px solid #efefef;
}
.comtent {
  padding: 10px 16px;
}
.com-input-class {
  background: #efefef;
  border-radius: 3px;
  padding: 5px 5px 5px 5px;
}
.place {
  color:red;
}
// wxml代码
<view class="overlay" style='display:{{showInput ? "block" : "none"}}' catchtouchmove="true" bindtap="onCancel"></view>
<view class="comInput" style="bottom: {{isBlur ? blurBottom : bottom}}px" wx:if="{{showInput}}">
  <view class="top hairline">
    <view class="cancel" bindtap="onCancel">取消</view>
    <view class="title">发表观点</view>
    <view class="release" bindtap="onRelease">发布</view>
  </view>
  <view class="comtent">{{title}}</view>
  <view style="padding:0 10px 10px 10px;">
    <van-field
      value="{{ inputValue }}"
      placeholder="内容经过官方合规性审查通过后对所有人可见"
      border="{{ false }}"
      cursor-spacing="95"
      input-class="border"
      bind:change="onChange"
      autosize="{{autoSize}}"
      bind:keyboardheightchange="keyboardheightchange"
      maxlength="200"
      adjust-position="{{false}}"
      arrow-direction="left"
      fixed="{{true}}"
      type="textarea"
      bind:focus="onFoucs"
      bind:blur="onBlur"
      bind:linechange="onLineChange"
      show-word-limit
      auto-focus
      focus
      placeholder-class="place"
      input-class="com-input-class"
      show-confirm-bar="{{ false }}"
    />
  </view>
</view>
// js代码
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    inputValue: String,
    title: String,
    showInput: {
      value: false,
      type: Boolean
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    autoSize: {
      maxHeight: 100,
      minHeight: 100
    },
    show: false,
    value: "",
    bottom: 0,
    isBlur: true,
    blurBottom: 0
  },
  /**
   * 组件的方法列表
   */
  methods: {
    keyboardheightchange (e) {
      // this.setData({
      //   bottom: e.detail.height
      // })
    },
    onFoucs (e) {
      this.setData({
        bottom: e.detail.height,
        isBlur: false
      })
    },
    onLineChange (e) {
    },
    onBlur (e) {
      this.setData({
        isBlur: true
      })
    },
    onShowInput () {
      this.setData({
        show: true
      })
    },
    onCancel () {
      console.log("点击了取消")
      this.setData({
        show: false
      })
      this.triggerEvent("close")
    },
    onRelease () {
      if (!this.data.inputValue.trim()) 
      return  wx.showToast({
          title: "请填写评论内容",
          icon: 'none',
          duration: 2000
        })
      this.triggerEvent("onOneComment", this.data.inputValue)
      this.onClose()
    },
    onChange (e) {
      this.data.inputValue = e.detail
      this.triggerEvent("onInput", e.detail)
    },
    onClose () {
      this.setData({
        show: false
      })
      this.triggerEvent("close")
    }
  }
})

到此这篇关于微信小程序vant 输入框问题的文章就介绍到这了,更多相关微信小程序vant 输入框内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • BootStrap自定义popover,点击区域隐藏功能的实现

    BootStrap自定义popover,点击区域隐藏功能的实现

    下面小编就为大家分享一篇BootStrap自定义popover,点击区域隐藏功能的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • js中eval方法详解之eval方法的初级应用

    js中eval方法详解之eval方法的初级应用

    js中eval()函数可计算某个字符串,下面这篇文章主要给大家介绍了关于js中eval方法详解之eval方法的初级应用的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • js回文数的4种判断方法示例

    js回文数的4种判断方法示例

    这篇文章主要给大家介绍了关于js回文数的4种判断方法,文中通过示例代码介绍的非常详细,对大家学习或者使用js具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • javascript中闭包closure的深入讲解

    javascript中闭包closure的深入讲解

    这篇文章主要给大家介绍了关于javascript中闭包closure的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 使用D3.js创建物流地图的示例代码

    使用D3.js创建物流地图的示例代码

    本篇文章主要介绍了使用D3.js创建物流地图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • JavaScript中Function详解

    JavaScript中Function详解

    函数是由关键字function、函数名加一组参数及置于大括号中需要执行的一段语义定义的。今天我们就来详细讲解一下JavaScript中的Function。
    2015-02-02
  • 深入理解JavaScript 参数按值传递

    深入理解JavaScript 参数按值传递

    本篇文章主要介绍了深入理解JavaScript 参数按值传递,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • 一文详解uniapp中如何使用easycom自定义组件

    一文详解uniapp中如何使用easycom自定义组件

    easycom是uniapp的一种组件自动引入的规则,使用这种规则可以使满足规则的组件无需注册直接使用,下面这篇文章主要给大家介绍了关于uniapp中如何使用easycom自定义组件的相关资料,需要的朋友可以参考下
    2023-05-05
  • js页面跳转常用的几种方式

    js页面跳转常用的几种方式

    js实现页面跳转的几种方式,需要的朋友可以参考下。
    2010-11-11
  • js实现双击单元格变成文本输入框效果代码

    js实现双击单元格变成文本输入框效果代码

    单击单元格,即可将其变为文本框,方便编辑测试
    2008-04-04

最新评论