詳解微信小程序?qū)崿F(xiàn)仿微信聊天界面(各種細(xì)節(jié)處理)
本文介紹了微信小程序?qū)崿F(xiàn)仿微信聊天界面,分享給大家,具體如下:
下面先來看看效果
為實(shí)現(xiàn)這樣的效果,首先要解決兩個(gè)問題:
1.點(diǎn)擊輸入框彈出軟鍵盤后,將已有的少許聊天內(nèi)容彈出,導(dǎo)致看不到的問題;
2.鍵盤彈出或收起時(shí),聊天消息沒有自動(dòng)滾到最底部。
首先解決第二個(gè)問題,自動(dòng)滾動(dòng)到最底部,這很簡單,這里提供三種方法(推薦第三種):
1.計(jì)算每條消息的最大高度,設(shè)置scroll-top=(單條msg最大高度 * msg條數(shù))px。
2.用 將展示msg的目標(biāo)scroll-view包裹,
通過js獲取到該view的實(shí)際高度:
var that = this; var query = wx.createSelectorQuery(); query.select('.scrollMsg').boundingClientRect(function(rect) { that.setData({ scrollTop: rect.height+'px'; }); }).exec();
3.(推薦)將所有msg都編號(hào)如:msg-0,msg-1,msg-2… 直接鎖定最后一條msg,滾動(dòng)到那里。
- 在scroll-view中添加:
scroll-into-view='{{toView}}'
, - 在wx:for后面添加:
wx:for-index="index"
, - 在每個(gè)msg布局中添加:
id='msg-{{index}}'
,
最后直接:
this.setData({ toView: 'msg-' + (msgList.length - 1) })
到這里第二個(gè)問題解決了,那么我們回過來解決第一個(gè)問題:
(點(diǎn)擊輸入框彈出軟鍵盤后,將已有的少許聊天內(nèi)容彈出,導(dǎo)致看不到的問題)
1.首先我們需要將input的自動(dòng)向上推給關(guān)掉,這里有個(gè)坑:
在input組件中添加:adjust-position='{{false}}'
,
而不是:adjust-position='false'
。
這么做雖然不再向上推,但卻導(dǎo)致了軟鍵盤彈起時(shí),會(huì)遮擋屏幕下部分的消息。
2.如何解決軟鍵盤彈起時(shí),會(huì)遮擋屏幕下部分的消息?
當(dāng)軟鍵盤彈起時(shí),將scroll-view的高度縮短至軟鍵盤遮擋不到的屏幕上方部分,當(dāng)軟鍵盤收起時(shí),再將scroll-view的高度還原,這樣解決了遮擋問題。
提示:
input中的bindfocus='focus'
可獲取軟鍵盤高度并監(jiān)聽軟鍵盤彈起,bindblur='blur'
可監(jiān)聽軟鍵盤收起,var windowHeight = wx.getSystemInfoSync().windowHeight;
可獲得屏幕高度。
scrollHeight(滾動(dòng)條高度) = windowHeight(屏幕高度) - 軟鍵盤高度;
最后將input組件放在軟鍵盤上面就完成了。
各位要不要代碼?
contact.js:
// pages/contact/contact.js const app = getApp(); var inputVal = ''; var msgList = []; var windowWidth = wx.getSystemInfoSync().windowWidth; var windowHeight = wx.getSystemInfoSync().windowHeight; var keyHeight = 0; /** * 初始化數(shù)據(jù) */ function initData(that) { inputVal = ''; msgList = [{ speaker: 'server', contentType: 'text', content: '歡迎來到英雄聯(lián)盟,敵軍還有30秒到達(dá)戰(zhàn)場,請做好準(zhǔn)備!' }, { speaker: 'customer', contentType: 'text', content: '我怕是走錯(cuò)片場了...' } ] that.setData({ msgList, inputVal }) } /** * 計(jì)算msg總高度 */ // function calScrollHeight(that, keyHeight) { // var query = wx.createSelectorQuery(); // query.select('.scrollMsg').boundingClientRect(function(rect) { // }).exec(); // } Page({ /** * 頁面的初始數(shù)據(jù) */ data: { scrollHeight: '100vh', inputBottom: 0 }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function(options) { initData(this); this.setData({ cusHeadIcon: app.globalData.userInfo.avatarUrl, }); }, /** * 生命周期函數(shù)--監(jiān)聽頁面顯示 */ onShow: function() { }, /** * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作 */ onPullDownRefresh: function() { }, /** * 頁面上拉觸底事件的處理函數(shù) */ onReachBottom: function() { }, /** * 獲取聚焦 */ focus: function(e) { keyHeight = e.detail.height; this.setData({ scrollHeight: (windowHeight - keyHeight) + 'px' }); this.setData({ toView: 'msg-' + (msgList.length - 1), inputBottom: keyHeight + 'px' }) //計(jì)算msg高度 // calScrollHeight(this, keyHeight); }, //失去聚焦(軟鍵盤消失) blur: function(e) { this.setData({ scrollHeight: '100vh', inputBottom: 0 }) this.setData({ toView: 'msg-' + (msgList.length - 1) }) }, /** * 發(fā)送點(diǎn)擊監(jiān)聽 */ sendClick: function(e) { msgList.push({ speaker: 'customer', contentType: 'text', content: e.detail.value }) inputVal = ''; this.setData({ msgList, inputVal }); }, /** * 退回上一頁 */ toBackClick: function() { wx.navigateBack({}) } })
contact.wxml:
<!--pages/contact/contact.wxml--> <view> <scroll-view scroll-y scroll-into-view='{{toView}}' style='height: {{scrollHeight}};'> <!-- <view class='scrollMsg'> --> <block wx:key wx:for='{{msgList}}' wx:for-index="index"> <!-- 單個(gè)消息1 客服發(fā)出(左) --> <view wx:if='{{item.speaker=="server"}}' id='msg-{{index}}' style='display: flex; padding: 2vw 11vw 2vw 2vw;'> <view style='width: 11vw; height: 11vw;'> <image style='width: 11vw; height: 11vw; border-radius: 10rpx;' src='../../images/contact_member.png'></image> </view> <view style='width: 4vw; height: 11vw; margin-left: 0.5vw; display: flex; align-items: center; z-index: 9;'> <image style='width: 4vw;' src='../../images/left_msg.png' mode='widthFix'></image> </view> <view class='leftMsg'>{{item.content}}</view> </view> <!-- 單個(gè)消息2 用戶發(fā)出(右) --> <view wx:else id='msg-{{index}}' style='display: flex; justify-content: flex-end; padding: 2vw 2vw 2vw 11vw;'> <view class='rightMsg'>{{item.content}}</view> <view style='width: 4vw; height: 11vw; margin-right: 0.5vw; display: flex; align-items: center; z-index: 9;'> <image style='width: 4vw;' src='../../images/right_msg.png' mode='widthFix'></image> </view> <view style='width: 11vw; height: 11vw;'> <image style='width: 11vw; height: 11vw; border-radius: 10rpx;' src='{{cusHeadIcon}}'></image> </view> </view> </block> <!-- </view> --> <!-- 占位 --> <view style='width: 100%; height: 18vw;'></view> </scroll-view> <view class='inputRoom' style='bottom: {{inputBottom}}'> <image style='width: 7vw; margin-left: 3.2vw;' src='../../images/pic_icon.png' mode='widthFix'></image> <input bindconfirm='sendClick' adjust-position='{{false}}' value='{{inputVal}}' confirm-type='send' bindfocus='focus' bindblur='blur'></input> </view> </view>
contact.wxss:
/* pages/contact/contact.wxss */ page { background-color: #f1f1f1; } .inputRoom { width: 100vw; height: 16vw; border-top: 1px solid #cdcdcd; background-color: #f1f1f1; position: fixed; bottom: 0; display: flex; align-items: center; z-index: 20; } input { width: 76vw; height: 9.33vw; background-color: #fff; border-radius: 40rpx; margin-left: 2vw; padding: 0 3vw; font-size: 28rpx; color: #444; } .leftMsg { font-size: 35rpx; color: #444; line-height: 7vw; padding: 2vw 2.5vw; background-color: #fff; margin-left: -1.6vw; border-radius: 10rpx; z-index: 10; } .rightMsg { font-size: 35rpx; color: #444; line-height: 7vw; padding: 2vw 2.5vw; background-color: #96EB6A; margin-right: -1.6vw; border-radius: 10rpx; z-index: 10; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序websocket聊天室的實(shí)現(xiàn)示例代碼
- 微信小程序websocket實(shí)現(xiàn)聊天功能
- 微信小程序WebSocket實(shí)現(xiàn)聊天對話功能
- 微信小程序?qū)崟r(shí)聊天WebSocket
- 微信小程序?qū)崿F(xiàn)即時(shí)通信聊天功能的實(shí)例代碼
- 使用node.js實(shí)現(xiàn)微信小程序?qū)崟r(shí)聊天功能
- 微信小程序與AspNetCore SignalR聊天實(shí)例代碼
- 微信小程序?qū)崿F(xiàn)聊天對話(文本、圖片)功能
- 微信小程序 聊天室簡單實(shí)現(xiàn)
- 微信小程序websocket實(shí)現(xiàn)即時(shí)聊天功能
相關(guān)文章
Typescript學(xué)習(xí)之接口interface舉例詳解
TypeScript是JavaScript的一個(gè)超集,主要提供了類型系統(tǒng)和對ES6+的支持,TypeScript的核心原則之一是對值所具有的結(jié)構(gòu)進(jìn)行類型檢查,這篇文章主要給大家介紹了關(guān)于Typescript學(xué)習(xí)之接口interface的相關(guān)資料,需要的朋友可以參考下2024-03-03bootstrap滾動(dòng)監(jiān)控器使用方法解析
這篇文章主要為大家詳細(xì)解析了bootstrap滾動(dòng)監(jiān)控器使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01JS是否可以跨文件同時(shí)控制多個(gè)iframe頁面的應(yīng)用技巧
這篇文章給大家詳細(xì)介紹了JS是否可以跨文件同時(shí)控制多個(gè)iframe頁面的應(yīng)用技巧,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2007-12-12不使用jquery實(shí)現(xiàn)js打字效果示例分享
js打字效果示例js打字效果示例,data-period設(shè)置從打字返回刪字的時(shí)間,data-rotate可加減中英文詞語,不用jquery支持2014-01-01解決layui使用layui-icon出現(xiàn)默認(rèn)圖標(biāo)的問題
今天小編就為大家分享一篇解決layui使用layui-icon出現(xiàn)默認(rèn)圖標(biāo)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09利用JavaScript實(shí)現(xiàn)簡單的網(wǎng)頁時(shí)鐘
這篇文章主要介紹了利用JavaScript實(shí)現(xiàn)簡單的網(wǎng)頁時(shí)鐘,主要使用了js的日期對象,實(shí)現(xiàn)的時(shí)候先創(chuàng)建一個(gè)日期對象,并進(jìn)行網(wǎng)頁布局,對時(shí)間獲取之后將時(shí)間填入對應(yīng)的標(biāo)簽內(nèi)。然后使用多線程實(shí)現(xiàn)時(shí)鐘的變動(dòng),需要的朋友可以參考一下2022-02-02