This article mainly introduces the relevant code of jquery imitating WeChat chat interface in detail. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
First take a look at our renderings.
#The color may be a bit inappropriate, but most of the basic functions are achieved. That is, you are talking to your deskmate, and the messages you send are on the left side of your device and on the right side of his device.
First write the overall framework and put two boxes in a large container, which are the left and right interfaces. Then each box contains three parts: header, content area, and bottom. Just write on one side and paste and copy on the other side.
First define a large
to hold the left and right boxes.
<p id = "main"> //左侧聊天界面 <p id = "box"> <p id = "top"><span>你</span></p> <p id = "content"> <select multiple="multiple" id="leftcontent"> </select> </p> <p id = "bottom"> <input type = "text" class = "sendText" id = "leftText" /> <input type = "button" id = "leftdBtn" class="sendBtn" value = "发送"> </p> </p> //右侧聊天界面 <p id = "box"> <p id = "top"><span>同桌</span></p> <p id = "content"> <select multiple="multiple" id="rightcontent"> </select> </p> <p id = "bottom"> <input type = "text" class = "sendText" id = "rightText" /> <input type = "button" id = "rightBtn" class="sendBtn" value = "发送"> </p> </p> </p>
First of all, the codes for these two boxes can be directly copied and pasted. You must also pay attention to the following differences: The ids in
<p id = "content"> <select multiple="multiple" id="rightcontent"> </select> </p>
select are different. We usually use
option1
option2
option3
like this. The select tag is used here when you and your deskmate chat for a whole screen. It has a scroll bar to slide up and down to see what you talked about. Then add some css styles on the basis of the above, so that the interface effect will come out.
The next step is to write the jquery code. First of all, think about whether what you say on your side should appear on the right side of your device and on the left side of your deskmate's device?
We first control sending messages on the left side of your interface. After writing the text, press the send button to make it appear on the right side of your interface and also on the left side of your deskmate's device.
We need to follow the following steps to achieve:
1. Get the contents of the text box you entered.
2. Generate an option tag.
2.1 The style of the generated tag, that is, the generated span tag is positioned and displayed on the right side of your device.
2.2 Insert the content of the generated tag, that is, insert the content in the text box
3. Append the option tag to your select.
4. Position and display the option label on the left side of your deskmate's device.
5. Clear the contents of the text box.
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(""); } }
Similarly, when you display the equipment at your deskmate, it is similar to the one on the left.
Just write it yourself.
After writing the message functions sent on the left and right sides, the message cannot be sent at this time because the event has not been bound yet. First, there are two ways to send messages:
①. Button sending
Button sending requires binding events to the button
$("#leftdBtn").bind("click", sendLeft); $("#rightBtn").bind("click", sendRight);
②. Press Enter to send
$(document).keydown(function(event){ var txt1 = $("#leftText").val(); var txt2 = $("#rightText").val() if(event.keyCode == 13){ if( txt1.trim() != ""){ sendLeft(); } if(txt2.trim() != ""){ sendRight(); } } });
Finally attach the complete source code:
<!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> <p id = "main"> <p id = "box"> <p id = "top"><span>你</span></p> <p id = "content"> <select multiple="multiple" id="leftcontent"> </select> </p> <p id = "bottom"> <input type = "text" class = "sendText" id = "leftText" /> <input type = "button" id = "leftdBtn" class="sendBtn" value = "发送"> </p> </p> <p id = "box"> <p id = "top"><span>同桌</span></p> <p id = "content"> <select multiple="multiple" id="rightcontent"> </select> </p> <p id = "bottom"> <input type = "text" class = "sendText" id = "rightText" /> <input type = "button" id = "rightBtn" class="sendBtn" value = "发送"> </p> </p> </p> </body> </html>
Related recommendations:
Examples to explain how to imitate WeChat chat bubbles using CSS3
js original sound to implement a simple WeChat chat function
CSS3 Example of imitating WeChat chat bubble code
The above is the detailed content of Sharing examples of jquery imitating WeChat chat interface. For more information, please follow other related articles on the PHP Chinese website!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool