一款css3結合jquery的聊天界面

為了讓項目更加的“親民”,這種聊天的需求越來越常見,今天,我們就從第一步開始,寫一個簡單輕便復用性高的聊天界面
這次,不先上代碼,先上圖

Paste_Image.png

不急,下面會貼出全部的代碼!現(xiàn)在,咱們先來聊聊制作這個界面的主要幾個點
1、純CSS3實現(xiàn)聊天框小尖角,即邊框法

![Uploading WechatIMG8_908204.jpeg . . .]

  • 邊框法
    簡單點說,就是用兩個標簽或無標簽 – 使用:before與:after偽類,形成的兩個不同的邊框進行組合顯示實現(xiàn)的一些效果。
html代碼
<ul class="chat-thread"> 
   <li>我是用邊框法實現(xiàn)的指向右側(cè)的對話框噢~</li>
    <li>我是用邊框法實現(xiàn)的指向左側(cè)的對話框噢~</li>
</ul>
css3代碼
.chat-thread {    margin: 24px auto 0 auto;    padding: 0 20px 0 0;    list-style: none;    overflow-y: scroll;    overflow-x: hidden;}
.chat-thread li {    position: relative;    clear: both;    display: inline-block;    padding: 16px 40px 16px 20px;    margin: 0 0 20px 0;    font: 16px/20px 'Noto Sans', sans-serif;    border-radius: 10px;    background-color: rgba(25, 147, 147, 0.2);}
.chat-thread li:before {    position: absolute;    top: 0;    width: 50px;    height: 50px;    border-radius: 50px;    content: '';}
.chat-thread li:after {    position: absolute;    top: 15px;    content: '';    width: 0;    height: 0;    border-top: 15px solid rgba(25, 147, 147, 0.2);}
.chat-thread li:nth-child(odd) {    animation: show-chat-odd 0.15s 1 ease-in;    -moz-animation: show-chat-odd 0.15s 1 ease-in;    -webkit-animation: show-chat-odd 0.15s 1 ease-in;    float: right;    margin-right: 80px;    color: #0AD5C1;}
.chat-thread li:nth-child(odd):before {    right: -80px;}
.chat-thread li:nth-child(odd):after {    border-right: 15px solid transparent;    right: -15px;}
.chat-thread li:nth-child(even) {    animation: show-chat-even 0.15s 1 ease-in;    -moz-animation: show-chat-even 0.15s 1 ease-in;    -webkit-animation: show-chat-even 0.15s 1 ease-in;    float: left;    margin-left: 80px;    color: #0EC879;}
.chat-thread li:nth-child(even):before {    left: -80px;}
.chat-thread li:nth-child(even):after {    border-left: 15px solid transparent;    left: -15px;}
WechatIMG9.jpeg
html代碼
<div class="chat-thread">
    <span class="bot"></span>
    <span class="top"></span>
    我是用邊框法實現(xiàn)的背景白色的對話框噢
</div>
css代碼
.chat-thread{padding:10px 2px; border:5px solid #beceeb; position:relative;border-radius: 6px}
.chat-thread span{width:0; height:0; overflow:hidden; position:absolute;}
.chat-thread span.bot{    border-width:20px;    border-style:solid dashed dashed;    border-color:#beceeb transparent transparent;    left:80px;    bottom:-40px;}
.chat-thread span.top{    border-width:20px;    border-style:solid dashed dashed;    border-color:#ffffff transparent transparent;    left:80px;    bottom:-33px;}

2、保證新發(fā)出來的消息在可見框的最底部,即每次增加消息,回滾滾動條保持在底部

  • 將滾動條滾動到任意的位置
var container = $('div'), 
scrollTo = $('#row_8');
container.scrollTop(scrollTo.offset().top - container.offset().top + container.scrollTop());  
 // 或者
container.animate({
      scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop()
 });
  • 知道了如何將滾動條滾動到對應的位置了,那滾動到底部就不難了,即當前滾動的地方的窗口頂端到整個頁面頂端的距離
$box.append(createuser($textContent)).animate({scrollTop: $(document).height()}, 300);

3、如何使用?
很簡單,下載了代碼之后,(注:稱上圖有頭像的的一方為別人發(fā),沒有頭像的一方為自己發(fā))
調(diào)用chat(element,imgSrc,doctextContent)
三個參數(shù),默認值為undefind,其說明如下

WechatIMG10.jpeg

<script>
    $(document).ready(function(){ 
       //別人發(fā)
       chat("leftBubble","images/head_portrait.png","您好,歡迎關注博客:http://write.blog.csdn.net/postlist"); 
       $(".send-btn").click(function(){ 
             //自己發(fā),點擊了發(fā)送        
           chat("rightBubble");  
            //清空輸入框
            $('.chat-info').html(''); 
       }) 
   })
</script>

代碼下載:https://coding.net/u/Jill/p/dankal-jill/git

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 1、垂直對齊 如果你用CSS,則你會有困惑:我該怎么垂直對齊容器中的元素?現(xiàn)在,利用CSS3的Transform,...
    kiddings閱讀 3,296評論 0 11
  • 1、屬性選擇器:id選擇器 # 通過id 來選擇類名選擇器 . 通過類名來選擇屬性選擇器 ...
    Yuann閱讀 1,752評論 0 7
  • 只有遠離故鄉(xiāng)的人才算得上顛沛流離。 我好像從來沒有特意寫過我的故鄉(xiāng),不過我寫過的很多文字,都是在故鄉(xiāng)寫出來的。像從...
    半夏長安閱讀 363評論 13 3
  • 最近的夢都很“兇”且怪異。 有一天,夢見楊濤和可媽,后者沖出陡坡,一路滾跌,落到山腳的馬路上,許多汽車緊急停下來,...
    YH姚紅閱讀 209評論 0 0
  • 清晨五點醒來,寂靜的小區(qū),空氣微涼。我倚在陽臺的沙發(fā)里。想你。 常常會陷入這種莫名的情緒里,滿腦子胡思亂想,不知道...
    Cathy1001閱讀 382評論 1 3

友情鏈接更多精彩內(nèi)容