div+CSS制作類似微信對話氣泡效果的實(shí)例總結(jié)

在聊天的場景中,聊天內(nèi)容需要用到氣泡修飾,如下圖。下面一一講解。
一、圖片式:
第一個(gè)樣式是京東客服,氣泡的圓角和鉤子都是用了圖片。使用了一個(gè)table組合成了一個(gè)圓角的框框。lm樣式拼出了鉤子。
- <div class="jimi_lists clearfix">
- <div class="header_img jimi3 fl"></div>
- <table class="msg" cellspacing="0" cellpadding="0">
- <tbody><tr><td class="lt"></td><td class="tt"></td><td class="rt"></td></tr>
- <tr><td class="lm"><span></span></td><td class="mm"><span class="wel"><span class="visitor"><p>很抱歉,現(xiàn)在人工客服忙,讓小JIMI為您解答吧。</p></span></span></td><td class="rm"></td></tr>
- <tr><td class="lb"></td><td class="bm"></td><td class="rb"></td></tr><tr><td></td></tr>
- </tbody>
- </table>
- </div>
- <div class="customer_lists clearfix">
- <div class="header_img jimi3" style="background: url(img/mine.jpg) no-repeat center;">
- <div class="header_img_hover"></div>
- </div>
- <table class="msg" cellspacing="0" cellpadding="0"><tbody><tr><td class="lt"></td><td class="tt"></td><td class="rt"></td></tr><tr><td class="lm"></td><td class="mm">你好,我是stoneniqiu</td><td class="rm"><span></span></td></tr><tr><td class="lb"></td><td class="bm"></td><td class="rb"></td></tr><tr><td></td><td class="time"></td><td></td></tr></tbody></table>
- </div>
樣式:
- .jimi_lists {
- margin: 0 -10px 20px 10px;
- position: relative;
- }
- .jimi_lists .header_img.jimi3 {
- background: url(../img/jimi_50_3.png) no-repeat 0 0;
- }
- .jimi_lists .header_img {
- width: 50px;
- height: 50px;
- position: absolute;
- top: 0;
- left: 0;
- }
- .fl {
- float: left;
- }
- .jimi_lists .msg {
- margin-left: 59px;
- margin-right: 40px;
- margin-top: 2px;
- float: left;
- display: inline;
- }
- .jimi_lists .lt {
- background: url(../img/msg_bg_lr.png) no-repeat 0 0;
- width: 26px;
- height: 10px;
- }
- .jimi_lists .tt {
- background: url(../img/msg_bg_tb.png) repeat-x 0 -23px;
- height: 10px;
- }
- .jimi_lists .rt {
- background: url(../img/msg_bg_lr.png) no-repeat -35px 0;
- width: 10px;
- height: 10px;
- }
- .jimi_lists .lm {
- background: url(../img/msg_bg_lr.png) repeat-y -94px 0;
- width: 26px;
- vertical-align: top;
- position: relative;
- }
- .jimi_lists .mm {
- padding: 0 5px;
- background-color: #FFF;
- word-break: break-all;
- word-wrap: break-word;
- color: #333;
- line-height: 20px;
- }
- .jimi_lists .lm span {
- background: url(../img/msg_bg_lr.png) no-repeat 0 -14px;
- display: inline-block;
- width: 26px;
- height: 16px;
- position: absolute;
- top: 5px;
- }
- .jimi_lists .rm {
- background: url(../img/msg_bg_lr.png) repeat-y -113px 0;
- width: 10px;
- }
- .jimi_lists .lb {
- background: url(../img/msg_bg_lr.png) no-repeat 0 -43px;
- width: 10px;
- height: 10px;
- }
- .jimi_lists .bm {
- background: url(../img/msg_bg_tb.png) repeat-x 0 -25px;
- height: 10px;
- }
- .jimi_lists .rb {
- background: url(../img/msg_bg_lr.png) no-repeat -35px -43px;
- width: 10px;
- height: 10px;
- }
- .customer_lists {
- position: relative;
- margin: 0 12px 20px 20px;
- }
- .customer_lists .msg {
- float: rightright;
- margin-right: 73px;
- _margin-rightright: 38px;
- margin-top: 2px;
- }
- .customer_lists .lt {
- background: url(../img/msg_bg_lr.png) no-repeat -47px -0;
- width: 10px;
- height: 10px;
- }
- .customer_lists .tt {
- background: url(../img/msg_bg_tb.png) repeat-x 0 0;
- height: 10px;
- }
- .customer_lists .rt {
- background: url(../img/msg_bg_lr.png) no-repeat -63px 0;
- width: 26px;
- height: 10px;
- }
- .customer_lists .lm {
- background: url(../img/msg_bg_lr.png) repeat-y -126px 0;
- width: 10px;
- }
- .customer_lists .mm {
- padding: 1px 8px;
- background-color: #3897E7;
- word-break: break-all;
- word-wrap: break-word;
- color: #FFF;
- }
- .customer_lists .rm {
- background: url(../img/msg_bg_lr.png) repeat-y -129px 0;
- width: 26px;
- vertical-align: top;
- position: relative;
- }
- .customer_lists .lb {
- background: url(../img/msg_bg_lr.png) no-repeat -47px -36px;
- width: 10px;
- height: 10px;
- }
- .customer_lists .bm {
- background: url(../img/msg_bg_tb.png) repeat-x -106px -4px;
- height: 10px;
- }
- .customer_lists .rb {
- background: url(../img/msg_bg_lr.png) no-repeat -63px -36px;
- width: 26px;
- height: 10px;
- }
- .customer_lists .rm span {
- background: url(../img/msg_bg_lr.png) no-repeat -63px -14px;
- display: inline-block;
- width: 26px;
- height: 16px;
- position: absolute;
- top: 5px;
- }
- .customer_lists .header_img_hover {
- position: absolute;
- background: url(../img/avatar_mask_01.png);
- top: 0;
- width: 50px;
- height: 50px;
- }
- .customer_lists .header_img {
- position: absolute;
- top: 0;
- rightright: 14px;
- width: 50px;
- height: 50px;
- }
- .clearfix:after {
- content: " .";
- display: block;
- clear: both;
- visibility: hidden;
- height: 0;
- }
二、背景式:
第二種主要是使用了:before偽類畫出了三角形,然后用定位拼在了一起。
- <div class="jimi_lists clearfix">
- <div class="header_img jimi3 fl"></div>
- <div class="bkbubble right">
- <p>換個(gè)氣泡如何</p>
- </div>
- </div>
- <div class="customer_lists clearfix">
- <div class="header_img jimi3" style="background: url(img/mine.jpg) no-repeat center;">
- <div class="header_img_hover"></div>
- </div>
- <div class="bkbubble left">
- <p>這個(gè)不錯</p>
- </div>
- </div>
(至于畫三角形的方法稍后文章最后會總結(jié)一下)
樣式:
- .bkbubble.rightright p:before, .bkbubble.left p:before {
- width: 0;
- position: absolute;
- top: 12px;
- border-style: solid;
- content: "";
- }
- .bkbubble.left {
- text-align: rightright;
- }
- .bkbubble.rightright p:before {
- left: -12px;
- border-color: transparent #00bfff;
- border-width: 0 12px 12px 0;
- }
- .bkbubble.left p:before {
- rightright: -12px;
- border-color: transparent #00bfff;
- border-width: 0 0 12px 12px ;
- }
三、邊框式:
如果要求邊框顏色和背景顏色不一樣呢?上面的方法就不行了,因?yàn)橛?before畫三角形是利用border的原理,也就是說,在上面這種方式下,氣泡邊上的小三形是無法呈現(xiàn)兩種顏色的,因?yàn)樗挥幸粋€(gè)border的顏色。那就要想辦法湊出一個(gè)有邊框的三角形了。
- <div class="jimi_lists lim_operator clearfix">
- <div class="header_img jimi3 fl"></div>
- <div class="lim_bubble lim_shadow"><p class="lim_dot">您好,請問有什么可以幫到您?</p></div>
- <div class="lim_tale"><div class="radiusborder"></div></div>
- </div>
1.先畫一個(gè)小矩形。
- .lim_operator .lim_tale {
- left: -1px;
- background-color: #c8f064;
- border-color: #a5d01b;
- margin-left: 70px;
- }
- .lim_tale {
- position: absolute;
- width: 12px;
- height: 8px;
- overflow: hidden !important;
- top: 10px;
- z-index: 2;
- border-top-style: solid;
- border-top-width: 1px;
- }
2.畫出斜線。
css里面是沒有斜線的,這里是用了一個(gè)園的弧形拼出來的。在一個(gè)大圓上,截取一段弧,就是一條斜線了。
- .lim_operator .radiusborder {
- position: absolute;
- background-color: #EFF0F2;
- top: -29px;
- left: -94px;
- height: 160px;
- width: 160px;
- border-top-style: solid;
- border-top-width: 1px;
- border-right-style: solid;
- border-right-width: 1px;
- border-top-rightright-radius: 154px;
- border-color: #a5d01b;
- }
四、疊加式:
原理就是相當(dāng)于用before先畫出一個(gè)有色背景,再用after畫出一個(gè)白色背景,然后疊加錯開頂部1-2px,這樣就出現(xiàn)了一個(gè)有邊框有背景的三角形。web 微信就是這種做法
- .expression:before {
- content: '';
- position: absolute;
- left: 16px;
- top: 100%;
- margin-left: -7px;
- border: 7px solid transparent;
- border-top-color: #CFCFCF
- }
- .expression:after {
- content: '';
- position: absolute;
- left: 16px;
- top: 100%;
- margin-left: -7px;
- margin-top: -1px;
- border: 7px solid transparent;
- border-top-color: #FFF
- }
PS:三角形的簡單繪制:
1.CSS 邊框
這也是一個(gè)常用的使用方式,如tooltips信息提示框和下拉菜單。以上的示例,這是一個(gè)我最喜歡的方法創(chuàng)建小而且有用的三角形。
優(yōu)點(diǎn):
很容易的通過修改一些CSS代碼屬性值而更改顏色和大小
這是一個(gè)跨瀏覽器的解決方案。
缺點(diǎn):
這個(gè)方式使用的是border,所以你不能添加陰影、漸變、和其他一些CSS3效果
請記住,IE6是不支持透明邊界的-如果你關(guān)心這個(gè)問題
2.HTML 字符
它是基于使用可用的Unicode字符列表的字符。
優(yōu)點(diǎn)
它是一個(gè)跨瀏覽器的技術(shù)
您可以使用CSS3的text-shadow屬性添加陰影。
缺點(diǎn)
不能使用太多的CSS3效果,除了使用文字陰影。
在所有的瀏覽器,這是相當(dāng)不可能實(shí)現(xiàn)像素完美。
3.CSS 旋轉(zhuǎn)正方形
理論上,這種方式,你需要使用兩個(gè)內(nèi)容塊,但是,并沒有限制是使用兩個(gè)元素,所以可以使用一個(gè)元素加一個(gè)偽元素。
創(chuàng)建一個(gè)內(nèi)容里。例如100×100像素 – 這將包含旋轉(zhuǎn)塊。
旋轉(zhuǎn)包含的這個(gè)塊45度,從而獲得一個(gè)菱形
將菱形的塊向頂部便宜,然后設(shè)置溢出,設(shè)置父層容器截?cái)?br />There you go!
優(yōu)點(diǎn):
CSS3陰影,漸變等可以更多的使用
缺點(diǎn):
這個(gè)解決方案不是跨瀏覽器的,首先是因?yàn)镃SS3旋轉(zhuǎn)。
相關(guān)文章
- 下面小編就為大家?guī)硪黄狣IV+CSS 清除浮動常用方法總結(jié)。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-06
- 這篇文章主要為大家詳細(xì)介紹了CSS教程之div垂直居中的多種方法,還包括多行文本垂直居中的方法,感興趣的小伙伴們可以參考一下2016-05-05
關(guān)于div與>div的區(qū)別小結(jié)
下面小編就為大家?guī)硪黄P(guān)于div與>div的區(qū)別小結(jié)。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-04- Divxdra1031.Dll2016-04-30
- 圖片溢出div問題怎么解決?下面小編就為大家?guī)硪黄獔D片溢出div問題的快速解決方法推薦。希望對大家有所幫助。一起跟隨小編過來看看吧2016-04-28
如何讓一個(gè)div居于頁面正中間【實(shí)現(xiàn)方法】
下面小編就為大家?guī)硪黄绾巫屢粋€(gè)div居于頁面正中間【實(shí)現(xiàn)方法】。小編覺得挺不錯的,現(xiàn)在分享個(gè)大家,也給大家做個(gè)參考2016-04-15css三種方法實(shí)現(xiàn)div在瀏覽器水平居中
這篇文章主要介紹了css三種實(shí)現(xiàn)div在瀏覽器水平居中的方法,感興趣的小伙伴們可以參考一下2016-04-14一個(gè)div在瀏覽器水平居中的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄粋€(gè)div在瀏覽器水平居中的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-04-14兩個(gè)div疊加觸發(fā)事件發(fā)生閃爍問題的解決方法
下面小編就為大家?guī)硪黄獌蓚€(gè)div疊加觸發(fā)事件發(fā)生閃爍問題的解決方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-09