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

開(kāi)發(fā)過(guò)程中遇到的問(wèn)題有如下幾個(gè):
1.van-field 搭配 van-popup
個(gè)別手機(jī)彈出后會(huì)導(dǎo)致輸入框位置亂跳,問(wèn)題原因是van-popup多次彈出數(shù)據(jù)渲染會(huì)有一定問(wèn)題
2.van-field 搭配 van-overlay(遮罩)
遮罩彈出太慢,手機(jī)性能比較差的體驗(yàn)太差
3.IOS自動(dòng)推上去內(nèi)容跑掉
處理方案:
自義定遮罩,利用display進(jìn)行設(shè)置,手機(jī)性能差的也幾乎不會(huì)卡頓
參考的是網(wǎng)上一個(gè)小哥代碼:http://www.fzitv.net/javascript/2976631fc.htm
// wxml代碼
// catchtouchmove="true" 對(duì)遮罩的穿透處理
<view class="overlayInput" style='display:{{showInput ? "block" : "none"}}' catchtouchmove="true" bindtap="onCancel"></view>// wxss代碼
// 這邊代碼是之前百度網(wǎng)上一個(gè)哥們的代碼,
.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代碼如下,因?yàn)槊看吸c(diǎn)擊評(píng)論按鈕需要自動(dòng)吊起鍵盤,所以加一個(gè)wx:if,這樣每次autofoucus都會(huì)生效,我這邊是寫成一個(gè)組件,父級(jí)調(diào)用的時(shí)候傳值下來(lái)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ā)表觀點(diǎn)</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ò)官方合規(guī)性審查通過(guò)后對(duì)所有人可見(jiàn)"
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)化處理:
由于小程序本身性能問(wèn)題,每次鍵盤喚醒都可以看到很明顯的卡頓,這邊參考了微博小程序的做法,第一次彈起的時(shí)候記錄一下位置,下次彈起直接跳轉(zhuǎn)到對(duì)應(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ā)表觀點(diǎn)</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ò)官方合規(guī)性審查通過(guò)后對(duì)所有人可見(jiàn)"
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("點(diǎn)擊了取消")
this.setData({
show: false
})
this.triggerEvent("close")
},
onRelease () {
if (!this.data.inputValue.trim())
return wx.showToast({
title: "請(qǐng)?zhí)顚懺u(píng)論內(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 輸入框問(wèn)題的文章就介紹到這了,更多相關(guān)微信小程序vant 輸入框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uniapp抖音小程序一鍵獲取用戶手機(jī)號(hào)的示例代碼
文章介紹了如何在uniapp抖音小程序中通過(guò)點(diǎn)擊按鈕一鍵獲取用戶手機(jī)號(hào),encryptedData和iv通過(guò)點(diǎn)擊按鈕回傳,后端部分通過(guò)解密獲取手機(jī)號(hào),感興趣的朋友一起看看吧2024-12-12
JavaScript創(chuàng)建閉包的兩種方式的優(yōu)劣與區(qū)別分析
這篇文章主要介紹了JavaScript創(chuàng)建閉包的兩種方式的優(yōu)劣與區(qū)別分析的相關(guān)資料,需要的朋友可以參考下2015-06-06
使用bootstrap typeahead插件實(shí)現(xiàn)輸入框自動(dòng)補(bǔ)全之問(wèn)題及解決辦法
這篇文章主要介紹了使用bootstrap typeahead插件實(shí)現(xiàn)輸入框自動(dòng)補(bǔ)全之問(wèn)題及解決辦法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
在javascript中隨機(jī)數(shù) math random如何生成指定范圍數(shù)值的隨機(jī)數(shù)
本篇文章給大家介紹在javascript中隨機(jī)數(shù)math random如何生成指定范圍數(shù)值的隨機(jī)數(shù),由于math.random生成了一個(gè)偽隨機(jī)數(shù),之后還要經(jīng)過(guò)我們的后期處理。對(duì)隨機(jī)數(shù)math random感興趣的朋友一起了解了解吧2015-10-10
TypeScript中的類型聲明文件的使用實(shí)戰(zhàn)場(chǎng)景
本文詳細(xì)介紹了TypeScript中的類型聲明文件(.d.ts),包括其作用、來(lái)源、如何自動(dòng)生成、內(nèi)置聲明文件和外部類型聲明文件的使用,通過(guò)實(shí)戰(zhàn)場(chǎng)景,幫助讀者徹底理解并掌握類型聲明文件的使用,感興趣的朋友跟隨小編一起看看吧2026-02-02

