微信小程序?qū)崿F(xiàn)聊天界面發(fā)送功能(示例代碼)
.wxml
<scroll-view scroll-y="true" style="height: {{windowHeight}}px;"> <view wx:for="{{chatList}}" wx:for-index="index" wx:for-item="item" style="padding-top:{{index==0?30:0}}rpx"> <!-- 左邊:對方用戶 --> <view style="display: flex; align-items: flex-start; margin-bottom: 10px;padding: 10rpx 20rpx;" wx:if="{{item.id != userInfo.id}}"> <view> <image src="{{item.url}}" style="width: 35px; height: 35px; border-radius: 10px;"></image> </view> <view style="padding: 15rpx 20rpx;border-radius: 0 10px 10px 10px;background-color: #ffffff; margin-left: 10rpx; display: inline-block;"> <text>{{item.content}}</text> </view> </view> <!-- 右邊:當(dāng)前用戶 --> <view style="display: flex; align-items: flex-start; justify-content: flex-end;padding: 10rpx 20rpx;" wx:else> <view style="padding: 15rpx 20rpx; background-color: #7e66f6; border-radius: 10px 0 10px 10px; margin-right: 10rpx;color: #ffffff;"> <text>{{item.content}}</text> </view> <view> <image src="{{item.url}}" style="width: 35px; height: 35px; border-radius: 10px;"></image> </view> </view> </view> <!-- 底部輸入框及發(fā)送按鈕 --> <view style="position: fixed; bottom: {{bottomJP}}px; width: 100%; background-color: #fff; padding: 0 10rpx; box-shadow: 0 -2px 4px rgba(202, 202, 202, 0.1);height: 130rpx;display: flex;justify-content: space-around;align-items: center;padding-bottom: 30rpx;"> <image src="/images/yuyin.png" style="width: 30px; height: 30px; border-radius: 10px;"></image> <input style="height: 20rpx;width: 60%; padding: 15rpx; border: 1px solid rgb(247, 245, 245); border-radius: 10rpx;" confirm-type="send" adjust-position="{{false}}" bindfocus="getTelIptHeight" bindblur="getTelIptHeight" bindconfirm="sendContent" value="{{inputValue}}"/> <image src="/images/biaoqing.png" style="width: 30px; height: 30px; border-radius: 10px;"></image> <image src="/images/tupian.png" style="width: 30px; height: 30px; border-radius: 10px;margin-right: 20rpx;"></image> <!-- <button style="width: 150rpx; height: 80rpx; background-color: #007bff; color: #fff; border: none; border-radius: 10px; margin-left: 10rpx;">發(fā)送</button> --> </view> </scroll-view>
.js
const app = getApp() Page({ /** * 頁面的初始數(shù)據(jù) */ data: { tips: null, windowHeight: 0, windowWidtth: 0, bottomJP: 0, userInfo: {}, inputValue: '', chatList: [{ createTime: '2024-07-24 09:10:00', url: '/images/baochang.png', content: '哈哈哈', id: 8 }, { createTime: '2024-07-24 11:15:30', url: '/images/baochang.png', content: '你好啊', id: 9 }, { createTime: '2024-07-24 11:15:30', url: '/images/baochang.png', content: '你好啊', id: 9 }, ] }, getTelIptHeight(e) { console.log("height---------", e); if (e.type == 'blur') { this.setData({ bottomJP: 0 }) } else { this.setData({ bottomJP: e.detail.height }) } }, onLoad(options) { let that = this that.setData({ userInfo: wx.getStorageSync("userInfo") }) wx.getSystemInfo({ success: function (res) { console.log(res) that.setData({ windowHeight: res.windowHeight, windowWidtth: res.windowWidth, }); } }); wx.setNavigationBarTitle({ title: '動(dòng)態(tài)獲取用戶昵稱', }) }, send() { let info = { senderId: wx.getStorageSync("userInfo").id, recipientId: 100, chatContent: '你好啊~', chatContentType: 'text' } let that = this app.globalData.ws.send({ data: JSON.stringify(info), success: (res) => { console.log(res) that.setData({ tips: "發(fā)送信息成功" }) } }) }, sendContent(e) { let message = { createTime: '2024-07-24 11:15:30', url: '/images/baochang.png', content: e.detail.value, id: 9 } let list = this.data.chatList list.push(message) this.setData({ chatList: list, inputValue: '' }) } })
.wxss
page { background-color: #f4f5f7; }
.json
{ "usingComponents": {}, "navigationBarTitleText": "" }
到此這篇關(guān)于微信小程序?qū)崿F(xiàn)聊天界面,發(fā)送功能的文章就介紹到這了,更多相關(guān)小程序聊天發(fā)送功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js 采用delete實(shí)現(xiàn)繼承示例代碼
這篇文章主要介紹了js如何采用delete實(shí)現(xiàn)所謂的繼承,下面有個(gè)不錯(cuò)的示例,大家可以參考下2014-05-05JavaScript實(shí)現(xiàn)文本目標(biāo)字符替換和一鍵全部替換
這篇文章主要介紹了JavaScript實(shí)現(xiàn)文本目標(biāo)字符替換和一鍵全部替換,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06JavaScript如何將base64圖片轉(zhuǎn)化為URL格式
這篇文章主要給大家介紹了關(guān)于JavaScript如何將base64圖片轉(zhuǎn)化為URL格式的相關(guān)資料,Base64是一種編碼方式,而不是真正的加密方式,即使算Base64也僅用作一個(gè)簡單的加密來保護(hù)某些數(shù)據(jù),而真正的加密通常都比較繁瑣,需要的朋友可以參考下2023-07-07JS實(shí)現(xiàn)固定在右下角可展開收縮DIV層的方法
這篇文章主要介紹了JS實(shí)現(xiàn)固定在右下角可展開收縮DIV層的方法,右下角的div層可實(shí)現(xiàn)收縮與展開的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02JavaScript如何實(shí)現(xiàn)圖片懶加載(lazyload) 提高用戶體驗(yàn)(增強(qiáng)版)
這篇文章主要介紹了JavaScript如何實(shí)現(xiàn)圖片懶加載(lazyload) 提高用戶體驗(yàn)(增強(qiáng)版)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11javascript實(shí)現(xiàn)禁止鼠標(biāo)滾輪事件
這篇文章主要介紹了javascript實(shí)現(xiàn)禁止鼠標(biāo)滾輪事件的相關(guān)資料,需要的朋友可以參考下2015-07-07詳解使用Next.js構(gòu)建服務(wù)端渲染應(yīng)用
這篇文章主要介紹了詳解使用Next.js構(gòu)建服務(wù)端渲染應(yīng)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07