詳解微信小程序?qū)崿F(xiàn)仿微信聊天界面(各種細(xì)節(jié)處理)
本文介紹了微信小程序?qū)崿F(xiàn)仿微信聊天界面,分享給大家,具體如下:
下面先來(lái)看看效果

為實(shí)現(xiàn)這樣的效果,首先要解決兩個(gè)問(wèn)題:
1.點(diǎn)擊輸入框彈出軟鍵盤(pán)后,將已有的少許聊天內(nèi)容彈出,導(dǎo)致看不到的問(wèn)題;
2.鍵盤(pán)彈出或收起時(shí),聊天消息沒(méi)有自動(dòng)滾到最底部。
首先解決第二個(gè)問(wèn)題,自動(dòng)滾動(dòng)到最底部,這很簡(jiǎn)單,這里提供三種方法(推薦第三種):
1.計(jì)算每條消息的最大高度,設(shè)置scroll-top=(單條msg最大高度 * msg條數(shù))px。
2.用 將展示msg的目標(biāo)scroll-view包裹,
通過(guò)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è)問(wèn)題解決了,那么我們回過(guò)來(lái)解決第一個(gè)問(wèn)題:
(點(diǎn)擊輸入框彈出軟鍵盤(pán)后,將已有的少許聊天內(nèi)容彈出,導(dǎo)致看不到的問(wèn)題)
1.首先我們需要將input的自動(dòng)向上推給關(guān)掉,這里有個(gè)坑:
在input組件中添加:adjust-position='{{false}}',
而不是:adjust-position='false'。
這么做雖然不再向上推,但卻導(dǎo)致了軟鍵盤(pán)彈起時(shí),會(huì)遮擋屏幕下部分的消息。
2.如何解決軟鍵盤(pán)彈起時(shí),會(huì)遮擋屏幕下部分的消息?
當(dāng)軟鍵盤(pán)彈起時(shí),將scroll-view的高度縮短至軟鍵盤(pán)遮擋不到的屏幕上方部分,當(dāng)軟鍵盤(pán)收起時(shí),再將scroll-view的高度還原,這樣解決了遮擋問(wèn)題。
提示:
input中的bindfocus='focus'可獲取軟鍵盤(pán)高度并監(jiān)聽(tīng)軟鍵盤(pán)彈起,bindblur='blur'可監(jiān)聽(tīng)軟鍵盤(pán)收起,var windowHeight = wx.getSystemInfoSync().windowHeight;可獲得屏幕高度。
scrollHeight(滾動(dòng)條高度) = windowHeight(屏幕高度) - 軟鍵盤(pán)高度;
最后將input組件放在軟鍵盤(pán)上面就完成了。
各位要不要代碼?
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: '歡迎來(lái)到英雄聯(lián)盟,敵軍還有30秒到達(dá)戰(zhàn)場(chǎng),請(qǐng)做好準(zhǔn)備!'
},
{
speaker: 'customer',
contentType: 'text',
content: '我怕是走錯(cuò)片場(chǎng)了...'
}
]
that.setData({
msgList,
inputVal
})
}
/**
* 計(jì)算msg總高度
*/
// function calScrollHeight(that, keyHeight) {
// var query = wx.createSelectorQuery();
// query.select('.scrollMsg').boundingClientRect(function(rect) {
// }).exec();
// }
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
scrollHeight: '100vh',
inputBottom: 0
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function(options) {
initData(this);
this.setData({
cusHeadIcon: app.globalData.userInfo.avatarUrl,
});
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示
*/
onShow: function() {
},
/**
* 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽(tīng)用戶下拉動(dòng)作
*/
onPullDownRefresh: function() {
},
/**
* 頁(yè)面上拉觸底事件的處理函數(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);
},
//失去聚焦(軟鍵盤(pán)消失)
blur: function(e) {
this.setData({
scrollHeight: '100vh',
inputBottom: 0
})
this.setData({
toView: 'msg-' + (msgList.length - 1)
})
},
/**
* 發(fā)送點(diǎn)擊監(jiān)聽(tīng)
*/
sendClick: function(e) {
msgList.push({
speaker: 'customer',
contentType: 'text',
content: e.detail.value
})
inputVal = '';
this.setData({
msgList,
inputVal
});
},
/**
* 退回上一頁(yè)
*/
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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序websocket聊天室的實(shí)現(xiàn)示例代碼
- 微信小程序websocket實(shí)現(xiàn)聊天功能
- 微信小程序WebSocket實(shí)現(xiàn)聊天對(duì)話功能
- 微信小程序?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)聊天對(duì)話(文本、圖片)功能
- 微信小程序 聊天室簡(jiǎn)單實(shí)現(xiàn)
- 微信小程序websocket實(shí)現(xiàn)即時(shí)聊天功能
相關(guān)文章
Typescript學(xué)習(xí)之接口interface舉例詳解
TypeScript是JavaScript的一個(gè)超集,主要提供了類(lèi)型系統(tǒng)和對(duì)ES6+的支持,TypeScript的核心原則之一是對(duì)值所具有的結(jié)構(gòu)進(jìn)行類(lèi)型檢查,這篇文章主要給大家介紹了關(guān)于Typescript學(xué)習(xí)之接口interface的相關(guān)資料,需要的朋友可以參考下2024-03-03
bootstrap滾動(dòng)監(jiān)控器使用方法解析
這篇文章主要為大家詳細(xì)解析了bootstrap滾動(dòng)監(jiān)控器使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
JS是否可以跨文件同時(shí)控制多個(gè)iframe頁(yè)面的應(yīng)用技巧
這篇文章給大家詳細(xì)介紹了JS是否可以跨文件同時(shí)控制多個(gè)iframe頁(yè)面的應(yīng)用技巧,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2007-12-12
不使用jquery實(shí)現(xiàn)js打字效果示例分享
js打字效果示例js打字效果示例,data-period設(shè)置從打字返回刪字的時(shí)間,data-rotate可加減中英文詞語(yǔ),不用jquery支持2014-01-01
解決layui使用layui-icon出現(xiàn)默認(rèn)圖標(biāo)的問(wèn)題
今天小編就為大家分享一篇解決layui使用layui-icon出現(xiàn)默認(rèn)圖標(biāo)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
如何從JavaScript數(shù)組中刪除空對(duì)象
JS中數(shù)組是我們較為常用的一種數(shù)據(jù)結(jié)構(gòu),下面這篇文章主要給大家介紹了關(guān)于如何從JavaScript數(shù)組中刪除空對(duì)象的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
利用JavaScript實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁(yè)時(shí)鐘
這篇文章主要介紹了利用JavaScript實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁(yè)時(shí)鐘,主要使用了js的日期對(duì)象,實(shí)現(xiàn)的時(shí)候先創(chuàng)建一個(gè)日期對(duì)象,并進(jìn)行網(wǎng)頁(yè)布局,對(duì)時(shí)間獲取之后將時(shí)間填入對(duì)應(yīng)的標(biāo)簽內(nèi)。然后使用多線程實(shí)現(xiàn)時(shí)鐘的變動(dòng),需要的朋友可以參考一下2022-02-02

