This article shares the specific code of the WeChat applet to implement a simple chat room for your reference. The specific content is as follows cha.js // pages/chat/chat.js // Get the mini program instance let app = getApp(); Page({ /** * Initial data of the page */ 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:`Hello! ` } ], 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:'' }); // Send the chat content to the server, return after processing, and then put the returned data into chatlist}, getInput:function(e){ console.log(e.detail.value); this.setData({invalue:e.detail.value}); }, /** * Life cycle function--listen for page loading*/ onLoad: function (options) { console.log(app.globalData.userInfo.nickName); this.setData({ nickname:app.globalData.userInfo.nickName, avatar:app.globalData.userInfo.avatarUrl }); }, /** * Life cycle function - listen for the completion of the initial rendering of the page*/ onReady: function () { }, /** * Life cycle function--monitor page display*/ onShow: function () { }, /** * Life cycle function--listen for page hiding*/ onHide: function () { }, /** * Life cycle function--monitor page uninstallation*/ onUnload: function () { }, /** * Page related event processing function - listen to user pull-down action */ onPullDownRefresh: function () { }, /** * The function that handles the bottoming event on the page*/ onReachBottom: function () { }, /** * User clicks on the upper right corner to share*/ 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="Please enter the chat content" /> </view> <button type="primary" bindtap="sendMsg">Send</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; } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: MySQL 5.7.17 installation and configuration method graphic tutorial
Ubuntu 16.04 builds FTP server Install ftp Instal...
Today, I will answer these newbie questions: Build...
Preface Today, I was reviewing the creational pat...
Table of contents 1. charAt grammar parameter ind...
Part.0 Background The company's intranet serv...
Detailed explanation of replace into example in m...
For reference only for Python developers using Ub...
1. Enter the Docker official website First, go to...
1. To prohibit all IP addresses from accessing th...
Table of contents Proxy forwarding rules The firs...
1. In the control panel, uninstall all components...
MySQL previously had a query cache, Query Cache. ...
Text shadow text-shadow property effects: 1. Lowe...
Linux and Unix are multi-user operating systems, ...
1. Replication Principle The master server writes...