JavaScript實現(xiàn)簡易聊天對話框(加滾動條)
更新時間:2020年02月10日 13:05:33 作者:天蝎座的文子
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)簡易聊天對話框,附加滾動條功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
今天看了幾個JS的視頻,老師布置了一個編寫一個簡易聊天對話框的任務,沒有涉及到Ajax.主要實現(xiàn)了切換頭像模擬兩方的聊天情況,樣式比較簡單,后期可以進行美化。
需要注意的地方是我是用的ul li列表來實現(xiàn)元素的添加,這樣更利于樣式的設置,每添加一個對話框需要清除一下浮動,不然會出現(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>
頁面結果如圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaScript中函數(shù)聲明與函數(shù)表達式的區(qū)別詳解
可能很多朋友只知道兩種聲明方式一個是函數(shù)聲明一個是函數(shù)表達式,具體有什么不同沒能說得很好。事實上,JavaScript的解析器對函數(shù)聲明與函數(shù)表達式并不是一視同仁地對待的。下面看看這兩者到底有什么不同。2016-08-08javascript 制作坦克大戰(zhàn)游戲初步 圖片與代碼
javascript 制作坦克大戰(zhàn)游戲初步 圖片與代碼...2007-11-11