jquery仿微信聊天界面
首先看一下我们的效果图。
这个颜色可能搭配的有些不合适,但基本功能大都实现了。就是你和你同桌对话,你发的消息在你的左侧,而在他设备的右侧。
首先先写好整体的框架,在一个大容器中放两个盒子,分别是左侧和右侧的界面。然后每个盒子里面包含了三大部分:头部、内容区、和底部。只要写好一侧,另一侧进行粘贴复制就可以了。
首先定义一个大的
来盛放左右两个盒子。
<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实现加载更多"转圈圈"效果,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-10
- 最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
- 有时我们在页面上需要选择数值范围,如购物时选取价格区间,购买主机时自主选取CPU,内存大小配置等,使用直观的滑块条直接选取想要的数值大小即可,无需手动输入数值,操作简单又方便。HTML首先载入jQuery库文件以及jRange相关...2015-03-15
- 本文实例讲述了jQuery实现非常实用漂亮的select下拉菜单选择效果。分享给大家供大家参考,具体如下:先来看如下运行效果截图:在线演示地址如下:http://demo.jb51.net/js/2015/js-select-chose-style-menu-codes/具体代码如...2015-11-08
- 这篇文章主要为大家详细介绍了jQuery实现广告显示和隐藏动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-05
- 这篇文章主要介绍了jQuery获得字体颜色16位码的方法,涉及jQuery样式操作及正则表达式使用技巧,非常简单实用,需要的朋友可以参考下...2016-02-23
- 这篇文章主要介绍了jQuery事件绑定用法,结合实例形式较为详细的分析了jQuery事件绑定的实现原理与相关注意事项,并附带了相关绑定方法的使用说明,重点介绍了bind和live的区别,需要的朋友可以参考下...2016-01-21
- 本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
- JQuery使我们在开发Ajax应用程序的时候提高了效率,减少了许多兼容性问题,我们在Ajax项目中,遇到ajax异步获取数据出错怎么办,我们可以通过捕捉error事件来获取出错的信息。在没给大家介绍正文之前先给分享Jquery中AJAX参...2015-11-24
- 拜读一个开源框架,最想学到的就是设计的思想和实现的技巧。废话不多说,jquery这么多年了分析都写烂了,老早以前就拜读过,不过这几年都是做移动端,一直御用zepto, 最近抽出点时间把jquery又给扫一遍我也不会照本宣科的翻译...2014-05-31
jquery中常用的SET和GET$(”#msg”).html循环介绍
复制代码 代码如下: $(”#msg”).html(); //返回id为msg的元素节点的html内容。 $(”#msg”).html(”new content“); //将“new content” 作为html串写入id为msg的元素节点内容中,页面显示粗体的new content $(”...2013-10-13- 最近由于项目的需要,需要动态的添加和删除table中的tr,感觉用JS可以实现,但是在网上找了一下,单纯的自己写JS,感觉太麻烦,而且也不好维护。于是想到了最近学的jQuery。这篇文章给大家用实例介绍了jQuery动态添加与删除tr行的方法,有需要的朋友们可以参考借鉴。...2016-10-20
- 当页面打开时我们需要执行一些操作,这个时候如果我们选择使用jquery的话,需要重写他的3中方法,自我感觉没什么区 别,看个人喜好了,第二种感觉比较简单明了: 第一种: 复制代码 代码如下: <script type="text/javas...2014-06-07
jQuery Mobile开发中日期插件Mobiscroll使用说明
这篇文章主要介绍了jQuery Mobile开发中日期插件Mobiscroll使用说明,需要的朋友可以参考下...2016-03-03jQuery 1.9使用$.support替代$.browser的使用方法
jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support 。 在更新的 2.0 版本中,将不再支持 IE 6/7/8。 以后,如果用户需要支持 IE 6/7/8,只能使用 jQuery 1.9。 如果要全面支持 IE,并混合...2014-05-31jQuery遍历DOM的父级元素、子级元素和同级元素的方法总结
借助jQuery我们可以轻松地堆DOM元素进行向上、向下遍历以及同级的遍历,本文我们即来整理jQuery遍历DOM的父级元素、子级元素和同级元素的方法总结:...2016-07-25- 在DOM操作中,常常需要动态创建HTML内容,使文档在浏览器里的呈现效果发生变化,并且达到各种各样的人机交互目的....2016-04-27
- jquery中jquery.offset().top / left用于获取div距离窗口的距离,jquery.position().top / left 用于获取距离父级div的距离(必须是绝对定位的div)。 (1)先介绍jquery.offset().top / left css: 复制代码 代码如下: *{ mar...2013-10-13
- 本文实例讲述了jQuery实现有动画淡出效果的二级折叠菜单代码。分享给大家供大家参考,具体如下:这里介绍jQuery实现有动画淡出效果的二级折叠菜单代码,相当不错,因考虑功能的实现,所以没有怎么美化,不过这样也好,可以给大家更...2015-10-21
- 有时候我们为了省事就操作键盘组合键去代替使用鼠标,我们今天就使用JQuery实现Ctrl+Enter提交表单。我们发帖时,在内容输入框中输入完内容后,可以点击“提交”按钮来发表内容。可是,如果你够“懒”,你可以不用动鼠标,只需按...2015-10-23