如何创建像 Facebook Messenger 这样的聊天气泡
How to create chat bubbles like facebook Messenger
我该如何创建这样的聊天气泡。更具体地说,如何将一种类型的用户的两条或更多条连续消息作为一个整体分组到一个气泡中。例如对于发件人 - 第一条消息的右下边框为 0,中间的消息的右上、下边框为 0 边框半径,最后一条消息的右上边框为 0 边框半径。我必须使用 javascript 还是可以使用 css 来完成?
HTML 结构可以是
<ul>
<li class="him">By Other User</li>
<li class="me">By this User, first message</li>
<li class="me">By this User, secondmessage</li>
<li class="me">By this User, third message</li>
<li class="me">By this User, fourth message</li>
</ul>
我应该使用哪种 css class/styles?
这是一个相当基本的示例,但它应该可以解释您需要的所有基础知识。
大部分解决方案都在 +
adjacent sibling selector 内。在这种情况下,它用于对来自同一个人的连续多条消息应用不同的边框半径。
ul{
list-style: none;
margin: 0;
padding: 0;
}
ul li{
display:inline-block;
clear: both;
padding: 20px;
border-radius: 30px;
margin-bottom: 2px;
font-family: Helvetica, Arial, sans-serif;
}
.him{
background: #eee;
float: left;
}
.me{
float: right;
background: #0084ff;
color: #fff;
}
.him + .me{
border-bottom-right-radius: 5px;
}
.me + .me{
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
}
.me:last-of-type {
border-bottom-right-radius: 30px;
}
<ul>
<li class="him">By Other User</li>
<li class="me">By this User, first message</li>
<li class="me">By this User, secondmessage</li>
<li class="me">By this User, third message</li>
<li class="me">By this User, fourth message</li>
</ul>
我建议在创建文本气泡时阅读这篇文章以获取帮助:
https://www.templatemonster.com/blog/chat-bubbles-css-ui-tutorial/
至于开始和结束的气泡,使用JQuery 来根据它们的父容器识别和更改它们的CSS 属性。
如果你想要发送的图像,你需要将它们包裹在 li 内并在相对对象 (li) 内做一个向右浮动或绝对位置。
<ul class="ulContainer">
<li>test1</li>
<li>test1</li>
<li>test1</li>
</ul>
Css:
.ulContainer li{
width:200px;
height:20px;
background-color:#9abff9;
list-style-type:none;
margin:10px 0 10px 0;
padding: 3px 3px 3px 5px;
border-radius: 10px 2px 2px 10px;
}
使用以下脚本更改第一个和最后一个 li:
$('.ulContainer li:first').css('border-top-right-radius','10px');
$('.ulContainer li:last').css('border-bottom-right-radius','10px');
这是 JSFiddle:https://jsfiddle.net/s60dgfw2/
根据您的评论更新:
我相信这是您在不使用 JQuery 的情况下最接近您想要实现的目标的方法。您需要高级选择器,您只能通过 JQuery 从 .each() 语句中分组获得。或者通过添加多个 css 类 到列表。
请参阅 了解如何使用多个 CSS 类。
或见下文:
https://jsfiddle.net/5dcto0td/
.fancyContainer{
border: 1px solid #555;
position:relative;
width:300px;
padding:5px;
overflow:hidden;
}
.chatBox {
width: 300px;
list-style: none;
margin: 0 0 0 -40px;
position:0;
}
.chatBox li {
margin: 5px 0 5px 0;
padding: 3px 5px 3px 5px;
}
/*Set up initial chat element for .me*/
.chatBox .me {
min-height: 20px;
float:right;
clear: both;
background-color: #34a1ef;
border-radius: 10px 2px 2px 10px;
}
/*Set up initial chat element for .him*/
.chatBox .him {
min-height: 20px;
float:left;
clear: both;
background-color: #ddd;
border-radius: 2px 10px 10px 2px;
}
/*Set up grouped radius*/
.him + .me {
border-top-right-radius:10px;
}
.me + .him {
border-top-left-radius:10px;
}
.me + .me {
border-bottom-right-radius:2px;
}
.him + .him {
border-bottom-left-radius:2px;
}
/*Set up First and Last radius for .me*/
.chatBox > .me:first-child {
border-top-right-radius:10px;
}
.chatBox > .me:last-child{
border-bottom-right-radius:10px;
}
/*Set up First and Last radius for .him*/
.chatBox > .him:first-child{
border-top-left-radius:10px;
}
.chatBox > .him:last-child{
border-bottom-left-radius:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fancyContainer">
<ul class="chatBox">
<li class="him">Hello... This is a chatbox.</li>
<li class="me">Well well. I guess this is a chatbox.</li>
<li class="me">I'll have to talk about this some other time.</li>
<li class="me">No wait. I might change my mind</li>
<li class="him">Nonesense good sir! We'll have this talk right now and here.</li>
<li class="him">I Like...</li>
<li class="him">popsicles.</li>
<li class="me">I can't believe you've done this to me!</li>
</ul>
</div>
你需要一个开始和一个结束 class,像这样
li {
border: 1px solid black;
list-style-type: none;
margin: 2px 0;
padding: 2px 5px;
}
.him {
float: left;
border-radius: 0 10px 10px 0;
clear: both;
}
.me {
float: right;
border-radius: 10px 0 0 10px;
clear: both;
}
.him.start {
border-top-left-radius: 10px;
}
.him.end {
border-bottom-left-radius: 10px;
}
.me.start {
border-top-right-radius: 10px;
}
.me.end {
border-bottom-right-radius: 10px;
}
<ul>
<li class="him start">By Other User</li>
<li class="him">By Other User</li>
<li class="him end">By Other User</li>
<li class="me start">By this User, first message</li>
<li class="me">By this User, secondmessage</li>
<li class="me">By this User, third message</li>
<li class="me end">By this User, fourth message</li>
<li class="him start">By Other User</li>
<li class="him">By Other User</li>
<li class="him end">By Other User</li>
<li class="me start">By this User, first message</li>
<li class="me">By this User, secondmessage</li>
<li class="me">By this User, third message</li>
<li class="me end">By this User, fourth message</li>
</ul>
这是一个纯粹的 css 解决方案,但它取决于您在发送组的最后一条消息时检测和应用 chat__bubble--stop
class 的能力。不幸的是,无法使用伪 class :last-of-type
;正如其他人指出的那样,群组中的最后一条消息不一定是对话的最后一条。它还使用相邻的兄弟选择器 (+
)。
.chat {
list-style-type: none;
width: 20em;
}
.chat__bubble {
margin-bottom: 3px;
padding: 5px 10px;
clear: both;
border-radius: 10px 10px 2px 2px ;
}
.chat__bubble--rcvd {
background: #f2f2f2;
color: black;
float: left;
border-bottom-right-radius: 10px;
}
.chat__bubble--sent {
background: #0066ff;
color: white;
float: right;
border-bottom-left-radius: 10px;
}
.chat__bubble--sent + .chat__bubble--sent {
border-top-right-radius: 2px;
}
.chat__bubble--rcvd + .chat__bubble--rcvd {
border-top-left-radius: 2px;
}
.chat__bubble--stop {
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
}
<ul class="chat">
<li class="chat__bubble chat__bubble--rcvd chat__bubble--stop">What are you up to?</li>
<li class="chat__bubble chat__bubble--sent">Not much.</li>
<li class="chat__bubble chat__bubble--sent">Just writing some CSS.</li>
<li class="chat__bubble chat__bubble--sent">I just LOVE writing CSS.</li>
<li class="chat__bubble chat__bubble--sent chat__bubble--stop">Do you?</li>
<li class="chat__bubble chat__bubble--rcvd">Yeah!</li>
<li class="chat__bubble chat__bubble--rcvd">It's super fun.</li>
<li class="chat__bubble chat__bubble--rcvd chat__bubble--stop">... SUPER fun.</li>
</ul>
我该如何创建这样的聊天气泡。更具体地说,如何将一种类型的用户的两条或更多条连续消息作为一个整体分组到一个气泡中。例如对于发件人 - 第一条消息的右下边框为 0,中间的消息的右上、下边框为 0 边框半径,最后一条消息的右上边框为 0 边框半径。我必须使用 javascript 还是可以使用 css 来完成?
HTML 结构可以是
<ul>
<li class="him">By Other User</li>
<li class="me">By this User, first message</li>
<li class="me">By this User, secondmessage</li>
<li class="me">By this User, third message</li>
<li class="me">By this User, fourth message</li>
</ul>
我应该使用哪种 css class/styles?
这是一个相当基本的示例,但它应该可以解释您需要的所有基础知识。
大部分解决方案都在 +
adjacent sibling selector 内。在这种情况下,它用于对来自同一个人的连续多条消息应用不同的边框半径。
ul{
list-style: none;
margin: 0;
padding: 0;
}
ul li{
display:inline-block;
clear: both;
padding: 20px;
border-radius: 30px;
margin-bottom: 2px;
font-family: Helvetica, Arial, sans-serif;
}
.him{
background: #eee;
float: left;
}
.me{
float: right;
background: #0084ff;
color: #fff;
}
.him + .me{
border-bottom-right-radius: 5px;
}
.me + .me{
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
}
.me:last-of-type {
border-bottom-right-radius: 30px;
}
<ul>
<li class="him">By Other User</li>
<li class="me">By this User, first message</li>
<li class="me">By this User, secondmessage</li>
<li class="me">By this User, third message</li>
<li class="me">By this User, fourth message</li>
</ul>
我建议在创建文本气泡时阅读这篇文章以获取帮助: https://www.templatemonster.com/blog/chat-bubbles-css-ui-tutorial/
至于开始和结束的气泡,使用JQuery 来根据它们的父容器识别和更改它们的CSS 属性。 如果你想要发送的图像,你需要将它们包裹在 li 内并在相对对象 (li) 内做一个向右浮动或绝对位置。
<ul class="ulContainer">
<li>test1</li>
<li>test1</li>
<li>test1</li>
</ul>
Css:
.ulContainer li{
width:200px;
height:20px;
background-color:#9abff9;
list-style-type:none;
margin:10px 0 10px 0;
padding: 3px 3px 3px 5px;
border-radius: 10px 2px 2px 10px;
}
使用以下脚本更改第一个和最后一个 li:
$('.ulContainer li:first').css('border-top-right-radius','10px');
$('.ulContainer li:last').css('border-bottom-right-radius','10px');
这是 JSFiddle:https://jsfiddle.net/s60dgfw2/
根据您的评论更新:
我相信这是您在不使用 JQuery 的情况下最接近您想要实现的目标的方法。您需要高级选择器,您只能通过 JQuery 从 .each() 语句中分组获得。或者通过添加多个 css 类 到列表。
请参阅
或见下文:
https://jsfiddle.net/5dcto0td/
.fancyContainer{
border: 1px solid #555;
position:relative;
width:300px;
padding:5px;
overflow:hidden;
}
.chatBox {
width: 300px;
list-style: none;
margin: 0 0 0 -40px;
position:0;
}
.chatBox li {
margin: 5px 0 5px 0;
padding: 3px 5px 3px 5px;
}
/*Set up initial chat element for .me*/
.chatBox .me {
min-height: 20px;
float:right;
clear: both;
background-color: #34a1ef;
border-radius: 10px 2px 2px 10px;
}
/*Set up initial chat element for .him*/
.chatBox .him {
min-height: 20px;
float:left;
clear: both;
background-color: #ddd;
border-radius: 2px 10px 10px 2px;
}
/*Set up grouped radius*/
.him + .me {
border-top-right-radius:10px;
}
.me + .him {
border-top-left-radius:10px;
}
.me + .me {
border-bottom-right-radius:2px;
}
.him + .him {
border-bottom-left-radius:2px;
}
/*Set up First and Last radius for .me*/
.chatBox > .me:first-child {
border-top-right-radius:10px;
}
.chatBox > .me:last-child{
border-bottom-right-radius:10px;
}
/*Set up First and Last radius for .him*/
.chatBox > .him:first-child{
border-top-left-radius:10px;
}
.chatBox > .him:last-child{
border-bottom-left-radius:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fancyContainer">
<ul class="chatBox">
<li class="him">Hello... This is a chatbox.</li>
<li class="me">Well well. I guess this is a chatbox.</li>
<li class="me">I'll have to talk about this some other time.</li>
<li class="me">No wait. I might change my mind</li>
<li class="him">Nonesense good sir! We'll have this talk right now and here.</li>
<li class="him">I Like...</li>
<li class="him">popsicles.</li>
<li class="me">I can't believe you've done this to me!</li>
</ul>
</div>
你需要一个开始和一个结束 class,像这样
li {
border: 1px solid black;
list-style-type: none;
margin: 2px 0;
padding: 2px 5px;
}
.him {
float: left;
border-radius: 0 10px 10px 0;
clear: both;
}
.me {
float: right;
border-radius: 10px 0 0 10px;
clear: both;
}
.him.start {
border-top-left-radius: 10px;
}
.him.end {
border-bottom-left-radius: 10px;
}
.me.start {
border-top-right-radius: 10px;
}
.me.end {
border-bottom-right-radius: 10px;
}
<ul>
<li class="him start">By Other User</li>
<li class="him">By Other User</li>
<li class="him end">By Other User</li>
<li class="me start">By this User, first message</li>
<li class="me">By this User, secondmessage</li>
<li class="me">By this User, third message</li>
<li class="me end">By this User, fourth message</li>
<li class="him start">By Other User</li>
<li class="him">By Other User</li>
<li class="him end">By Other User</li>
<li class="me start">By this User, first message</li>
<li class="me">By this User, secondmessage</li>
<li class="me">By this User, third message</li>
<li class="me end">By this User, fourth message</li>
</ul>
这是一个纯粹的 css 解决方案,但它取决于您在发送组的最后一条消息时检测和应用 chat__bubble--stop
class 的能力。不幸的是,无法使用伪 class :last-of-type
;正如其他人指出的那样,群组中的最后一条消息不一定是对话的最后一条。它还使用相邻的兄弟选择器 (+
)。
.chat {
list-style-type: none;
width: 20em;
}
.chat__bubble {
margin-bottom: 3px;
padding: 5px 10px;
clear: both;
border-radius: 10px 10px 2px 2px ;
}
.chat__bubble--rcvd {
background: #f2f2f2;
color: black;
float: left;
border-bottom-right-radius: 10px;
}
.chat__bubble--sent {
background: #0066ff;
color: white;
float: right;
border-bottom-left-radius: 10px;
}
.chat__bubble--sent + .chat__bubble--sent {
border-top-right-radius: 2px;
}
.chat__bubble--rcvd + .chat__bubble--rcvd {
border-top-left-radius: 2px;
}
.chat__bubble--stop {
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
}
<ul class="chat">
<li class="chat__bubble chat__bubble--rcvd chat__bubble--stop">What are you up to?</li>
<li class="chat__bubble chat__bubble--sent">Not much.</li>
<li class="chat__bubble chat__bubble--sent">Just writing some CSS.</li>
<li class="chat__bubble chat__bubble--sent">I just LOVE writing CSS.</li>
<li class="chat__bubble chat__bubble--sent chat__bubble--stop">Do you?</li>
<li class="chat__bubble chat__bubble--rcvd">Yeah!</li>
<li class="chat__bubble chat__bubble--rcvd">It's super fun.</li>
<li class="chat__bubble chat__bubble--rcvd chat__bubble--stop">... SUPER fun.</li>
</ul>