国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jquery仿微信聊天界面

2019-11-19 16:39:14
字體:
來源:轉載
供稿:網友

首先看一下我們的效果圖。

這里寫圖片描述

這個顏色可能搭配的有些不合適,但基本功能大都實現了。就是你和你同桌對話,你發的消息在你的左側,而在他設備的右側。

首先先寫好整體的框架,在一個大容器中放兩個盒子,分別是左側和右側的界面。然后每個盒子里面包含了三大部分:頭部、內容區、和底部。只要寫好一側,另一側進行粘貼復制就可以了。

首先定義一個大的

來盛放左右兩個盒子。

<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);

②?;剀嚢l送

$(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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 余庆县| 剑河县| 商丘市| 永泰县| 黄冈市| 荆州市| 健康| 灯塔市| 澜沧| 南充市| 乐都县| 光山县| 滨州市| 清远市| 衡水市| 梓潼县| 盐亭县| 宣威市| 三亚市| 扎兰屯市| 无为县| 五原县| 科技| 原阳县| 任丘市| 宜城市| 台南市| 乡城县| 左云县| 新竹县| 天柱县| 万源市| 金川县| 项城市| 屏边| 德州市| 黄陵县| 吉木乃县| 蒲江县| 微博| 常熟市|