微信小程序vant?輸入框問題處理方案
在開發(fā)時(shí)候需要添加評論,點(diǎn)擊的時(shí)候從底部彈起,效果如下圖
開發(fā)過程中遇到的問題有如下幾個(gè):
1.van-field 搭配 van-popup
個(gè)別手機(jī)彈出后會導(dǎo)致輸入框位置亂跳,問題原因是van-popup多次彈出數(shù)據(jù)渲染會有一定問題
2.van-field 搭配 van-overlay(遮罩)
遮罩彈出太慢,手機(jī)性能比較差的體驗(yàn)太差
3.IOS自動(dòng)推上去內(nèi)容跑掉
處理方案:
自義定遮罩,利用display進(jìn)行設(shè)置,手機(jī)性能差的也幾乎不會卡頓
參考的是網(wǎng)上一個(gè)小哥代碼: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)上一個(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)擊評論按鈕需要自動(dòng)吊起鍵盤,所以加一個(gè)wx:if,這樣每次autofoucus都會生效,我這邊是寫成一個(gè)組件,父級調(diào)用的時(shí)候傳值下來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ī)性審查通過后對所有人可見" 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)化處理:
由于小程序本身性能問題,每次鍵盤喚醒都可以看到很明顯的卡頓,這邊參考了微博小程序的做法,第一次彈起的時(shí)候記錄一下位置,下次彈起直接跳轉(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ā)表觀點(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ī)性審查通過后對所有人可見" 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: "請?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實(shí)現(xiàn)仿FLASH效果的豎排導(dǎo)航代碼
這篇文章主要介紹了JS實(shí)現(xiàn)仿FLASH效果的豎排導(dǎo)航代碼,涉及JavaScript基于定時(shí)函數(shù)動(dòng)態(tài)設(shè)置頁面元素樣式的技巧,具有FLASH變換效果,需要的朋友可以參考下2015-09-09javascript驗(yàn)證香港身份證的格式或真實(shí)性
本文分享了利用javascript驗(yàn)證香港身份證的格式或真實(shí)性的代碼,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02JavaScript undefined及null區(qū)別實(shí)例解析
這篇文章主要介紹了JavaScript undefined及null區(qū)別實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07小程序獲取當(dāng)前位置加搜索附近熱門小區(qū)及商區(qū)的方法
這篇文章主要介紹了小程序獲取當(dāng)前位置加搜索附近熱門小區(qū)及商區(qū)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04原生js實(shí)現(xiàn)無限循環(huán)輪播圖效果
本文主要介紹了原生js實(shí)現(xiàn)無限循環(huán)輪播圖效果的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01分享JS代碼實(shí)現(xiàn)鼠標(biāo)放在輸入框上輸入框和圖片同時(shí)更換樣式
在一些網(wǎng)站我們會見到,當(dāng)鼠標(biāo)放在輸入框上輸入框和圖片同時(shí)更換樣式,那么基于js代碼是如何實(shí)現(xiàn)的呢?下面小編給大家解答下2016-09-09