微信小程序vant?輸入框問題處理方案
在開發(fā)時候需要添加評論,點擊的時候從底部彈起,效果如下圖

開發(fā)過程中遇到的問題有如下幾個:
1.van-field 搭配 van-popup
個別手機(jī)彈出后會導(dǎo)致輸入框位置亂跳,問題原因是van-popup多次彈出數(shù)據(jù)渲染會有一定問題
2.van-field 搭配 van-overlay(遮罩)
遮罩彈出太慢,手機(jī)性能比較差的體驗太差
3.IOS自動推上去內(nèi)容跑掉
處理方案:
自義定遮罩,利用display進(jìn)行設(shè)置,手機(jī)性能差的也幾乎不會卡頓
參考的是網(wǎng)上一個小哥代碼:http://www.dbjr.com.cn/javascript/2976631fc.htm
// wxml代碼
// catchtouchmove="true" 對遮罩的穿透處理
<view class="overlayInput" style='display:{{showInput ? "block" : "none"}}' catchtouchmove="true" bindtap="onCancel"></view>// wxss代碼
// 這邊代碼是之前百度網(wǎng)上一個哥們的代碼,
.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都會生效,我這邊是寫成一個組件,父級調(diào)用的時候傳值下來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">發(fā)表觀點</view>
<view class="release" bindtap="onRelease">發(fā)布</view>
</view>
<view class="comtent">{{title}}</view>
<view style="padding:0 10px 10px 10px;">
<van-field
value="{{ inputValue }}"
placeholder="內(nèi)容經(jīng)過官方合規(guī)性審查通過后對所有人可見"
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>優(yōu)化處理:
由于小程序本身性能問題,每次鍵盤喚醒都可以看到很明顯的卡頓,這邊參考了微博小程序的做法,第一次彈起的時候記錄一下位置,下次彈起直接跳轉(zhuǎn)到對應(yīng)位置
組件全部代碼如下,有需要可以自行提取使用,需要自己修改部分:
// 樣式
.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">發(fā)表觀點</view>
<view class="release" bindtap="onRelease">發(fā)布</view>
</view>
<view class="comtent">{{title}}</view>
<view style="padding:0 10px 10px 10px;">
<van-field
value="{{ inputValue }}"
placeholder="內(nèi)容經(jīng)過官方合規(guī)性審查通過后對所有人可見"
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
}
},
/**
* 組件的初始數(shù)據(jù)
*/
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: "請?zhí)顚懺u論內(nèi)容",
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")
}
}
})到此這篇關(guān)于微信小程序vant 輸入框問題的文章就介紹到這了,更多相關(guān)微信小程序vant 輸入框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS實現(xiàn)仿FLASH效果的豎排導(dǎo)航代碼
這篇文章主要介紹了JS實現(xiàn)仿FLASH效果的豎排導(dǎo)航代碼,涉及JavaScript基于定時函數(shù)動態(tài)設(shè)置頁面元素樣式的技巧,具有FLASH變換效果,需要的朋友可以參考下2015-09-09
JavaScript undefined及null區(qū)別實例解析
這篇文章主要介紹了JavaScript undefined及null區(qū)別實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
小程序獲取當(dāng)前位置加搜索附近熱門小區(qū)及商區(qū)的方法
這篇文章主要介紹了小程序獲取當(dāng)前位置加搜索附近熱門小區(qū)及商區(qū)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
分享JS代碼實現(xiàn)鼠標(biāo)放在輸入框上輸入框和圖片同時更換樣式
在一些網(wǎng)站我們會見到,當(dāng)鼠標(biāo)放在輸入框上輸入框和圖片同時更換樣式,那么基于js代碼是如何實現(xiàn)的呢?下面小編給大家解答下2016-09-09

