微信小程序?qū)崿F(xiàn)簡(jiǎn)單聊天室
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)簡(jiǎn)單聊天室的具體代碼,供大家參考,具體內(nèi)容如下
cha.js
// pages/chat/chat.js // 獲取小程序?qū)嵗? let app = getApp(); Page({ /** * 頁(yè)面的初始數(shù)據(jù) */ data: { nickname:'', avatar:'', chatlists:[ { nickname:'小林', avatar:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1591617971938&di=30d9f3b49a0d1b27fb4b61ea424f82c9&imgtype=0&src=http%3A%2F%2Fa-ssl.duitang.com%2Fuploads%2Fitem%2F201610%2F07%2F20161007135058_nUxji.jpeg', content:`你好呀!` } ], invalue:'' }, sendMsg:function(){ let _this = this; let obj = { nickname:_this.data.nickname, avatar:_this.data.avatar, content:_this.data.invalue }; let arr = _this.data.chatlists; arr.push(obj) _this.setData({ chatlists:arr, invalue:'' }); // 把聊天內(nèi)容發(fā)送到服務(wù)器,處理完成后返回,再把返回的數(shù)據(jù)放到chatlist里面 }, getInput:function(e){ console.log(e.detail.value); this.setData({invalue:e.detail.value}); }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載 */ onLoad: function (options) { console.log(app.globalData.userInfo.nickName); this.setData({ nickname:app.globalData.userInfo.nickName, avatar:app.globalData.userInfo.avatarUrl }); }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示 */ onShow: function () { }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏 */ onHide: function () { }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載 */ onUnload: function () { }, /** * 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽(tīng)用戶下拉動(dòng)作 */ onPullDownRefresh: function () { }, /** * 頁(yè)面上拉觸底事件的處理函數(shù) */ onReachBottom: function () { }, /** * 用戶點(diǎn)擊右上角分享 */ onShareAppMessage: function () { } })
chat.wxml
<block wx:for="{{chatlists}}" wx:for-index="ind" wx:for-item="chat" wx:key="ind"> <view class="chat self" wx:if="{{nickname == chat.nickname}}"> <view class="right"> <view class="content"> {{chat.content}} </view> </view> <view class="left"> <image class="avatar" src="{{chat.avatar}}"></image> </view> </view> <view class="chat" wx:else> <view class="left"> <image class="avatar" src="{{chat.avatar}}"></image> </view> <view class="right"> <view class="nickname">{{chat.nickname}}</view> <view class="content"> {{chat.content}} </view> </view> </view> </block> <view class="form"> <view class="weui-cell weui-cell_input"> <input class="weui-input" value="{{invalue}}" bindinput="getInput" placeholder="請(qǐng)輸入聊天內(nèi)容" /> </view> <button type="primary" bindtap="sendMsg">發(fā)送</button> </view>
chat.css
/* pages/chat/chat.wxss */ .avatar{ width: 130rpx; height: 130rpx; border-radius: 50%; } .chat{ display: flex; align-items: center; margin-top: 15px; } .self{ justify-content: flex-end; margin-top: 15px; } .left{ padding: 20rpx; align-self: flex-start; } .self .left{ padding-top: 0; } .right{ margin-left: 10px; } .right .content{ background-color: #eee; color: #123; font-size: 16px; /* border:1px solid #ddd; */ padding: 10px; line-height: 26px; margin-right: 10px; border-radius: 3px; position: relative; min-height: 20px; } .right .content::before{ content: ' '; display: block; width: 0; height: 0; border: 12px solid transparent; border-right-color:#eee; position: absolute; top: 10px; left: -23px; } .self .right .content::before{ border: 0; } .self .right .content::after{ content: ' '; display: block; width: 0; height: 0; border: 12px solid transparent; border-left-color:#1ad409; position: absolute; top: 10px; right: -23px; } .self .right .content{ background-color: #1ad409; } .form{ position: fixed; bottom: 0; background-color: #eee; width: 750rpx; display: flex; height: 39px; align-items: center; } .form input{ width: 600rpx; background-color: #fff; height: 36px; line-height: 36px; padding: 0 5px; } button{ width:65rpx; height:36px; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一個(gè)不錯(cuò)的用JavaScript實(shí)現(xiàn)的UBB編碼函數(shù)
一個(gè)不錯(cuò)的用JavaScript實(shí)現(xiàn)的UBB編碼函數(shù)...2007-03-03canvas簡(jiǎn)單快速的實(shí)現(xiàn)知乎登錄頁(yè)背景效果
本篇文章主要介紹了canvas簡(jiǎn)單快速實(shí)現(xiàn)知乎登錄頁(yè)背景效果的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05Javascript使用SWFUpload進(jìn)行多文件上傳
本篇文章主要解釋了使用SWFUpload進(jìn)行多文件上傳,這里整理了詳細(xì)的代碼,有需要的可以了解一下。2016-11-11微信小程序利用for循環(huán)解決內(nèi)容變更問(wèn)題
這篇文章主要介紹了微信小程序利用for循環(huán)解決內(nèi)容變更問(wèn)題 ,本文分步驟通過(guò)實(shí)例代碼詳解給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03JS實(shí)現(xiàn)圖片無(wú)間斷滾動(dòng)代碼匯總
這篇文章主要介紹了JS實(shí)現(xiàn)圖片無(wú)間斷滾動(dòng)代碼匯總,非常實(shí)用的特效代碼,需要的朋友可以參考下2014-07-07excel操作之Add Data to a Spreadsheet Cell
excel操作之Add Data to a Spreadsheet Cell...2007-06-06