溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaScript如何實現(xiàn)簡易聊天對話框

發(fā)布時間:2021-04-12 09:53:04 來源:億速云 閱讀:2583 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了JavaScript如何實現(xiàn)簡易聊天對話框,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

今天看了幾個JS的視頻,老師布置了一個編寫一個簡易聊天對話框的任務(wù),沒有涉及到Ajax.主要實現(xiàn)了切換頭像模擬兩方的聊天情況,樣式比較簡單,后期可以進行美化。

需要注意的地方是我是用的ul li列表來實現(xiàn)元素的添加,這樣更利于樣式的設(shè)置,每添加一個對話框需要清除一下浮動,不然會出現(xiàn)連續(xù)幾個對話框出現(xiàn)在一行的現(xiàn)象。

代碼如下:

<!DOCTYPE html>

<html>

<head>
<meta charset="utf-8">
<title>聊天對話框</title>
<style type="text/css">
#container{
width: 250px;
height: 350px;
border:1px solid #7b6b6b;
margin: 0 auto;
position: relative;

}

#content{
width: 250px;
height: 300px;
border-bottom: 1px solid #ccc;
overflow-y: auto;

}

#content ul{
margin: 0;
padding: 0;

}

#Img{
width: 30px;
height: 30px;
position: absolute;
left: 10px;
top: 310px;
border-radius: 15px;

}

#txt{
margin: 0;
position: absolute;
left: 50px;
top: 315px;
border-radius: 2px;
border:1px solid #ccc;
width: 133px;
height: 18px;

}

#btn{
margin-right: 10px;
position: absolute;
margin: 0;
left: 197px;
top: 314px;

}

#edit{
background: #ece7e766;
width: 250px;
height: 50px;

}

.showTxt{
width: auto;
height: auto;
max-width: 230px;
background: #008000a8;
border:0;
font-size: 15px;
color: white;
padding: 5px;
border-radius: 2px; 
word-break: break-all;
list-style: none;
margin-top: 5px;
display: list-item;

}

.left{
text-align: left;
margin-left: 50px;
float: left;

}

.right{
text-align: right;
margin-right: 50px;
float: right;

}

.showImg{
width: 26px;
height: 26px;
border-radius: 13px;

 

}

.leftImg{
left: 10px;
position: absolute;

}

.rightImg{
right: 10px;
position: absolute;

}

#scroll{
position: relative;

}

</style>
</head>
<body>

<div id="container">
<div id="content">
<div id="scroll">
<ul id="save"></ul>
</div>
</div>

<div id="edit">
<img src="1.jpg" id="Img">
<input type="text" name="" id="txt">
<input type="button" name="" value="發(fā)送" id="btn">
</div>
</div>
<script type="text/javascript">

 //獲取元素

var oCont=document.getElementById('content');
var oImg=document.getElementById('Img');
var oTxt=document.getElementById('txt');
var oBtn=document.getElementById('btn');
var oSTxt=document.getElementsByClassName('showTxt');
var oSave=document.getElementById('save');
var num=0;

 //切換頭像
oImg.οnclick=function(){
num++;
if(num%2==0)
oImg.src='1.jpg';
else
oImg.src='2.jpg';

}

 //發(fā)送事件
oBtn.οnclick= function(){
addCon();

}

function addCon(){ 
//定義需要添加的元素
var newLi=document.createElement("li");
var newImg=document.createElement('img');
//判斷聊天的對象是哪一方,文字框出現(xiàn)在左邊還是右邊
 if(num%2==0){
//添加對話框
newLi.innerHTML=oTxt.value;
newLi.className='showTxt right';
oSave.appendChild(newLi); 
oTxt.value='';
 //添加頭像
newImg.src=oImg.src;
newImg.className='showImg rightImg';
newLi.appendChild(newImg); 

 //清除浮動
var div = document.createElement('div');
 div.style = 'clear:both';
 oSave.appendChild(div);
 }else{
 newLi.innerHTML=oTxt.value;
newLi.className='showTxt left';
oSave.appendChild(newLi); 
oTxt.value='';
newImg.src=oImg.src;
newImg.className='showImg leftImg';
newLi.appendChild(newImg);
var div = document.createElement('div');
 div.style = 'clear:both';
 oSave.appendChild(div);

 }

}

</script>
</body>
</html>

頁面結(jié)果如圖:

JavaScript如何實現(xiàn)簡易聊天對話框

感謝你能夠認真閱讀完這篇文章,希望小編分享的“JavaScript如何實現(xiàn)簡易聊天對話框”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI