js模拟群聊天php,jquery仿微信聊天界面实例分享

本文主要为大家详细介绍了jquery仿微信聊天界面的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。

首先看一下我们的效果图。

d8462c12d6fb3169ed9744c9b0c35751.png

这个颜色可能搭配的有些不合适,但基本功能大都实现了。就是你和你同桌对话,你发的消息在你的左侧,而在他设备的右侧。

首先先写好整体的框架,在一个大容器中放两个盒子,分别是左侧和右侧的界面。然后每个盒子里面包含了三大部分:头部、内容区、和底部。只要写好一侧,另一侧进行粘贴复制就可以了。

首先定义一个大的

来盛放左右两个盒子。

//左侧聊天界面

//右侧聊天界面

同桌

首先这两个盒子的代码不是复制粘贴就直接可以的。还必须注意以下不同:

select中的id得不同。我们一般都是

option1

option2

option3

这样使用。而在这儿使用select标签是当你和你同桌聊了一屏的天时,它有滚动条来 上下滑动看你们都聊了些什么。再上面的基础上增加一些css样式,这样界面效果就出来了。

接下来就是要写jquery代码的时候了。首先想一下你在你这边说的话既要出现在你的设备右侧,又要出现在你同桌设备的左侧?

我们先对你的界面左侧进行发消息控制,在写了文本之后,按发送按钮让它出现在你界面的右侧,同时也出现在你同桌设备的左侧。

我们要按照以下步骤来实现:

1。获得你输入的文本框中的内容。

2。生成一个option标签。

2.1 生成标签的样式即生成的span标签在你的设备的右侧进行定位并 显示。

2.2 对生成的标签进行内容的插入即插入文本框中的内容

3。将option标签追加到你的select中。

4。将option标签在你同桌设备的左侧进行定位显示。

5。清除文本框中的内容。

function sendLeft(){

//1.获得你输入的文本框中的内容。

var text = $("#leftText").val();

//2。生成一个span标签。

var option = $("``");

// 2.1 生成标签的样式即生成的span标签在你的设备的右侧进行定位并显示。

var len = text.length;

option.css("width", len * 15 + "px");

option.css("marginLeft", 350 - len * 15 - 60 + "px");

//2.2 生成标签的内容

option.html(text);

//3. 将内容追加到select中。

$("#leftcontent").append(option);

//4. 追加生成的标签(右侧)

var option1 = $("");

option1.addClass("optionRight");

option1.css("width", len * 15 + "px");

option1.css("marginLeft", 10 +"px");

option1.html(text);

$("#rightcontent").append(option1);

//5. 清除文本框的内容

$("#leftText").val("");

}

}

同样再对你同桌的设备方进行显示的时候,和左侧的大同小异。

自己写一下就可以。

在写了左侧和右侧发送的消息函数之后,此时还不能进行消息发送,因为还没有进行事件绑定。首先发送消息有两种方式:

①。按钮发送

按钮发送就需要为按钮绑定事件

$("#leftdBtn").bind("click", sendLeft);

$("#rightBtn").bind("click", sendRight);

②。回车发送

$(document).keydown(function(event){

var txt1 = $("#leftText").val();

var txt2 = $("#rightText").val()

if(event.keyCode == 13){

if( txt1.trim() != ""){

sendLeft();

}

if(txt2.trim() != ""){

sendRight();

}

}

});

最后附上完整的源代码:

模仿微信聊天

*{

margin: 0px;

padding: 0px;

}

#main{

width: 90%;

margin: 10px auto;

}

#box{

float: left;

margin:20px 120px;

}

#top{

width: 310px;

padding: 10px 20px;

color: white;

background-color: lightgreen;

font-size: 18px;

font-family: "微软雅黑";

font-weight: bold;

}

#content{

background-color: white;

}

select{

width: 350px;

height: 470px;

background-color: white;

padding: 10px;

border:2px solid red;

}

#bottom{

width: 310px;

background-color: red;

padding: 10px 20px;

}

.sendText{

height: 25px;

width: 210px;

font-size: 16px;

}

.sendBtn{

width: 65px;

height: 30px;

float: right;

background-color: gold;

color: white;

text-align: center;

font-size: 18px;

}

span{

background-color: lightgreen;

color: #000;

padding: 10px 30px;

}

option{

padding: 5px 10px;

margin-top:10px;

border-radius:5px;

width: 10px;

min-height: 20px;

}

.optionRight{

background-color: lightgreen;

}

.optionLeft{

background-color: lightblue;

}

$(function(){

$("#leftdBtn").bind("click", sendLeft);

$("#rightBtn").bind("click", sendRight);

function sendLeft(){

//1. 获取输入框中的内容

var text = $("#leftText").val();

//2. 生成标签

var option = $("");

option.addClass("optionLeft");

//2.1 生成标签的样式

var len = text.length;

//option.css("width", len * 15 + "px","marginLeft", 350 - len * 15 - 60 + "px")

option.css("width", len * 15 + "px");

option.css("marginLeft", 350 - len * 15 - 60 + "px");

//2.2 生成标签的内容

option.html(text);

//3. 将内容追加到select中。

$("#leftcontent").append(option);

//4. 追加生成的标签(右侧)

var option1 = $("");

option1.addClass("optionRight");

option1.css("width", len * 15 + "px");

option1.css("marginLeft", 10 +"px");

option1.html(text);

$("#rightcontent").append(option1);

//5. 清除文本框的内容

$("#leftText").val("");

}

function sendRight(){

//1. 获取输入框中的内容

var text = $("#rightText").val();

//2. 生成标签

var option = $("");

option.addClass("optionLeft");

//2.1 生成标签的样式

var len = text.length;

//option.css("width", len * 15 + "px","marginLeft", 350 - len * 15 - 60 + "px")

option.css("width", len * 15 + "px");

option.css("marginLeft", 350 - len * 15 - 60 + "px");

//2.2 生成标签的内容

option.html(text);

//3. 将内容追加到select中。

$("#rightcontent").append(option);

//4. 追加生成的标签(右侧)

var option1 = $("");

option1.addClass("optionRight");

option1.css("width", len * 15 + "px");

option1.css("marginLeft", 10 +"px");

option1.html(text);

$("#leftcontent").append(option1);

$("#rightText").val("");

}

$(document).keydown(function(event){

var txt1 = $("#leftText").val();

var txt2 = $("#rightText").val()

if(event.keyCode == 13){

if( txt1.trim() != ""){

sendLeft();

}

if(txt2.trim() != ""){

sendRight();

}

}

});

})

同桌

相关推荐:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里提供一个基于jQuery的H5聊天界面实例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Chat Room</title> <style> #chat-container { display: flex; flex-direction: column; height: 100%; border: 1px solid #ccc; border-radius: 5px; overflow: hidden; } #chat-header { padding: 10px; background-color: #f6f6f6; border-bottom: 1px solid #ccc; text-align: center; } #chat-messages { flex: 1; overflow-y: scroll; padding: 10px; } #chat-messages ul { list-style: none; margin: 0; padding: 0; } #chat-messages li { margin: 10px 0; } #chat-messages li.received { text-align: left; } #chat-messages li .message-content { display: inline-block; padding: 5px 10px; background-color: #f6f6f6; border-radius: 5px; } #chat-input { display: flex; align-items: center; padding: 10px; background-color: #f6f6f6; } #chat-input input[type="text"] { flex: 1; padding: 5px; border-radius: 5px; border: none; } #chat-input button { margin-left: 10px; padding: 5px 10px; border-radius: 5px; border: none; background-color: #007bff; color: #fff; cursor: pointer; } #chat-input button:hover { background-color: #0069d9; } </style> </head> <body> <div id="chat-container"> <div id="chat-header"> <h2 id="contact-name">John Doe</h2> </div> <div id="chat-messages"> <ul id="message-list"></ul> </div> <div id="chat-input"> <input type="text" id="message-input" placeholder="Type your message here..."> <button id="send-button">Send</button> </div> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { var messages = [ { id: 1, content: 'Hi there!', isReceived: true }, { id: 2, content: 'How are you?', isReceived: false } ]; // Render messages function renderMessages() { var messageList = $('#message-list'); messageList.empty(); messages.forEach(function(message) { var messageClass = message.isReceived ? 'received' : ''; var messageHtml = '<li class="' + messageClass + '"><div class="message-content">' + message.content + '</div></li>'; messageList.append(messageHtml); }); messageList.scrollTop(messageList[0].scrollHeight); } // Send message function sendMessage() { var messageInput = $('#message-input'); var messageContent = messageInput.val().trim(); if (messageContent !== '') { messages.push({ id: messages.length + 1, content: messageContent, isReceived: false }); messageInput.val(''); renderMessages(); } } // Bind events $('#send-button').click(sendMessage); $('#message-input').keyup(function(event) { if (event.keyCode === 13) { sendMessage(); } }); // Initial render renderMessages(); }); </script> </body> </html> ``` 这个界面与Vue.js版本的类似,包含了一个聊天头部、聊天消息列表和一个输入框(包含发送按钮)。您可以根据您的实际需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值