🐙
DM機能をline風に実装してみた
はじめに
DM機能の実装でひと段落つきそろそろレイアウトを気にしたいなと考え、
いろんなサイトを渡り歩いた結果、なんとなく形になったためメモ程度に書いてみました。
イメージ
CSSのコード
/*以下、①背景色など*/
.line-bc {
padding: 20px 10px;
width: 100%;
margin: 15px auto;
text-align: left;
font-size: 14px;
background: #6dbec8;
}
/*以下、②左側のコメント*/
.balloon6 {
width: 100%;
margin: 10px 0;
overflow: hidden;
}
.balloon6 .chatting {
width: 100%;
text-align: right;
}
.says {
display: inline-block;
position: relative;
margin: 0 0 0 50px;
padding: 10px;
max-width: 250px;
border-radius: 12px;
background: #edf1ee;
}
.says:after {
content: "";
display: inline-block;
position: absolute;
top: 3px;
left: -19px;
border: 8px solid transparent;
border-right: 18px solid #edf1ee;
-webkit-transform: rotate(35deg);
transform: rotate(35deg);
}
.says p {
margin: 0;
padding: 0;
}
/*以下、③右側の緑コメント*/
.mycomment {
margin: 10px 0;
}
.mycomment p {
display: inline-block;
position: relative;
margin: 0 10px 0 0;
padding: 8px;
max-width: 250px;
border-radius: 12px;
background: #30e852;
font-size: 15px;
}
.mycomment p:after {
content: "";
position: absolute;
top: 3px;
right: -19px;
border: 8px solid transparent;
border-left: 18px solid #30e852;
-webkit-transform: rotate(-35deg);
transform: rotate(-35deg);
}
htmlのコード
<div class="line-bc"><!--①LINE会話全体を囲う-->
<!--②左コメント始-->
<% if @messages.present? %>
<% @messages.each do |m| %>
<% if @myUserId == m.user.id %>
<p style="text-align: right;"><%= m.user.name %></p>
<div class="balloon6">
<div class="chatting">
<div class="mycomment">
<p><%= m.message %></p>
</div>
</div>
</div>
<% else %>
<p style="text-align: left;"><%= m.user.name %></p>
<div class="says">
<p><%= m.message %></p>
</div>
<% end %>
<% end %>
<% end %>
</div>
if文を用いて現在のユーザーであれば右にコメントを表示。
相手のユーザーのメッセージは左に表示するように場合分けを行いました。
Discussion