最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript實(shí)現(xiàn)簡易聊天對(duì)話框(加滾動(dòng)條)

 更新時(shí)間:2020年02月10日 13:05:33   作者:天蝎座的文子  
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡易聊天對(duì)話框,附加滾動(dòng)條功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

代碼如下:

<!DOCTYPE html>

<html>

<head>
<meta charset="utf-8">
<title>聊天對(duì)話框</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');
//判斷聊天的對(duì)象是哪一方,文字框出現(xiàn)在左邊還是右邊
 if(num%2==0){
//添加對(duì)話框
newLi.innerHTML=oTxt.value;
newLi.className='showTxt right';
oSave.appendChild(newLi); 
oTxt.value='';
 //添加頭像
newImg.src=oImg.src;
newImg.className='showImg rightImg';
newLi.appendChild(newImg); 

 //清除浮動(dòng)
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é)果如圖:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

镇赉县| 本溪市| 苍溪县| 房产| 金溪县| 全椒县| 怀远县| 邹城市| 平江县| 云安县| 瑞昌市| 杭州市| 裕民县| 舞钢市| 盐山县| 巴东县| 北安市| 宜君县| 龙川县| 磐安县| 府谷县| 辰溪县| 安泽县| 从化市| 荃湾区| 泉州市| 呼图壁县| 杂多县| 陇川县| 葫芦岛市| 华蓥市| 晋城| 孝昌县| 阿瓦提县| 宝清县| 项城市| 兴义市| 法库县| 西峡县| 广东省| 什邡市|