jquery仿微信聊天界面
发布时间 - 2026-01-11 01:00:11 点击率:次首先看一下我们的效果图。
这个颜色可能搭配的有些不合适,但基本功能大都实现了。就是你和你同桌对话,你发的消息在你的左侧,而在他设备的右侧。
首先先写好整体的框架,在一个大容器中放两个盒子,分别是左侧和右侧的界面。然后每个盒子里面包含了三大部分:头部、内容区、和底部。只要写好一侧,另一侧进行粘贴复制就可以了。
首先定义一个大的
来盛放左右两个盒子。
<div id = "main"> //左侧聊天界面 <div id = "box"> <div id = "top"><span>你</span></div> <div id = "content"> <select multiple="multiple" id="leftcontent"> </select> </div> <div id = "bottom"> <input type = "text" class = "sendText" id = "leftText" /> <input type = "button" id = "leftdBtn" class="sendBtn" value = "发送"> </div> </div> //右侧聊天界面 <div id = "box"> <div id = "top"><span>同桌</span></div> <div id = "content"> <select multiple="multiple" id="rightcontent"> </select> </div> <div id = "bottom"> <input type = "text" class = "sendText" id = "rightText" /> <input type = "button" id = "rightBtn" class="sendBtn" value = "发送"> </div> </div> </div>
首先这两个盒子的代码不是复制粘贴就直接可以的。还必须注意以下不同:
<div id = "content"> <select multiple="multiple" id="rightcontent"> </select> </div>
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 = $("`<option></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 = $("<option></option>");
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();
}
}
});
最后附上完整的源代码:
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8"/>
<title>模仿微信聊天</title>
<script type="text/javascript" src = "http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<style type="text/css">
*{
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;
}
</style>
<script>
$(function(){
$("#leftdBtn").bind("click", sendLeft);
$("#rightBtn").bind("click", sendRight);
function sendLeft(){
//1. 获取输入框中的内容
var text = $("#leftText").val();
//2. 生成标签
var option = $("<option></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 = $("<option></option>");
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></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 = $("<option></option>");
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();
}
}
});
})
</script>
</head>
<body>
<div id = "main">
<div id = "box">
<div id = "top"><span>你</span></div>
<div id = "content">
<select multiple="multiple" id="leftcontent">
</select>
</div>
<div id = "bottom">
<input type = "text" class = "sendText" id = "leftText" />
<input type = "button" id = "leftdBtn" class="sendBtn" value = "发送">
</div>
</div>
<div id = "box">
<div id = "top"><span>同桌</span></div>
<div id = "content">
<select multiple="multiple" id="rightcontent">
</select>
</div>
<div id = "bottom">
<input type = "text" class = "sendText" id = "rightText" />
<input type = "button" id = "rightBtn" class="sendBtn" value = "发送">
</div>
</div>
</div>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# jquery微信聊天界面
# jquery微信聊天
# jquery聊天界面
# jquery添加div实现消息聊天框
# jQuery实现聊天对话框
# jquery实现聊天机器人
# JS(jQuery)实现聊天接收到消息语言自动提醒功能详解【提示“您有新的消息请注意查收”】
# 基于jQuery实现简单人工智能聊天室
# 使用jQuery调用XML实现无刷新即时聊天
# javascript和jQuery实现网页实时聊天的ajax长轮询
# PHP+jquery+ajax实现即时聊天功能实例
# JavaScript/jQuery、HTML、CSS 构建 Web IM 远程及时聊天通信程序
# jQuery实现简易聊天框
# 框中
# 出现在
# 和你
# 写了
# 绑定
# 聊了
# 写好
# 就可以
# 文本框
# 都是
# 还没有
# 在他
# 你在
# 当你
# 而在
# 三大
# 些什么
# 这两个
# 微软
# 看你
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
linux写shell需要注意的问题(必看)
Python正则表达式进阶教程_复杂匹配与分组替换解析
Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】
如何用AI帮你把自己的生活经历写成一个有趣的故事?
深圳防火门网站制作公司,深圳中天明防火门怎么编码?
Laravel怎么实现搜索高亮功能_Laravel结合Scout与Algolia全文检索【实战】
如何在云主机快速搭建网站站点?
HTML5打空格有哪些误区_新手常犯的空格使用错误【技巧】
Laravel如何实现API版本控制_Laravel API版本化路由设计策略
Win11怎样安装网易有道词典_Win11安装词典教程【步骤】
零服务器AI建站解决方案:快速部署与云端平台低成本实践
SQL查询语句优化的实用方法总结
Python文件流缓冲机制_IO性能解析【教程】
如何在腾讯云服务器上快速搭建个人网站?
5种Android数据存储方式汇总
uc浏览器二维码扫描入口_uc浏览器扫码功能使用地址
Laravel如何配置和使用队列处理异步任务_Laravel队列驱动与任务分发实例
JavaScript 输出显示内容(document.write、alert、innerHTML、console.log)
如何用免费手机建站系统零基础打造专业网站?
如何解决hover在ie6中的兼容性问题
如何在 React 中条件性地遍历数组并渲染元素
如何在IIS服务器上快速部署高效网站?
如何在建站宝盒中设置产品搜索功能?
怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?
免费网站制作appp,免费制作app哪个平台好?
Win11怎么关闭透明效果_Windows11辅助功能视觉效果设置
如何快速搭建二级域名独立网站?
Laravel如何使用模型观察者?(Observer代码示例)
Laravel中DTO是什么概念_在Laravel项目中使用数据传输对象(DTO)
今日头条微视频如何找选题 今日头条微视频找选题技巧【指南】
Android使用GridView实现日历的简单功能
电视网站制作tvbox接口,云海电视怎样自定义添加电视源?
Laravel模型关联查询教程_Laravel Eloquent一对多关联写法
怎么用AI帮你设计一套个性化的手机App图标?
Laravel广播系统如何实现实时通信_Laravel Reverb与WebSockets实战教程
如何快速搭建个人网站并优化SEO?
php增删改查怎么学_零基础入门php数据库操作必知基础【教程】
如何彻底删除建站之星生成的Banner?
香港服务器建站指南:免备案优势与SEO优化技巧全解析
WordPress 子目录安装中正确处理脚本路径的完整指南
如何在Windows环境下新建FTP站点并设置权限?
如何快速搭建虚拟主机网站?新手必看指南
原生JS实现图片轮播切换效果
香港服务器租用每月最低只需15元?
装修招标网站设计制作流程,装修招标流程?
微信小程序 input输入框控件详解及实例(多种示例)
PHP正则匹配日期和时间(时间戳转换)的实例代码
魔毅自助建站系统:模板定制与SEO优化一键生成指南
Laravel如何与Pusher实现实时通信?(WebSocket示例)
IOS倒计时设置UIButton标题title的抖动问题

