<div id="viewport"></div>
* {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
body {
background-color: #f1f1f1;
display: flex;
font-family: 'Lato', sans-serif;
font-size: 0.875rem;
font-weight: 400;
color: #2c3e50;
height: 100vh;
overflow-y: hidden;
}
/* CUSTOM SCROLLBAR FOR CHATBOX */
.chats::-webkit-scrollbar { width: 0.125rem; }
.chats::-webkit-scrollbar-thumb { background: #CFD8DC; }
.chats::-webkit-scrollbar-thumb:hover { background: #B0BEC5; }
/* INPUT TEXT PLACEHOLDER CUSTOMIZE */
::-webkit-input-placeholder { color: #B0BEC5; }
::-moz-placeholder { color: #B0BEC5; }
:-ms-input-placeholder { color: #B0BEC5; }
:-moz-placeholder { color: #B0BEC5; }
#viewport {
display: flex;
flex: 1;
justify-content: center;
align-items: center;
}
#viewport > .chatbox {
position: relative;
display: table;
float: left;
margin: 1rem;
width: 20rem;
height: 36rem;
background-color: white;
box-shadow: 0 0.25rem 2rem rgba(38,50,56, 0.1);
overflow: hidden;
}
#viewport > .chatbox > .chats {
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
display: table-cell;
vertical-align: bottom;
padding: 1rem;
margin-bottom: 2.875rem;
overflow: auto;
}
ul { padding: 0; }
ul > li {
position: relative;
list-style: none;
display: block;
margin-top: 1.5rem;
margin: 1rem 0;
transition: 0.5s all;
}
ul > li:after {
display: table;
content: '';
clear: both;
}
.msg {
max-width: 85%;
display: inline-block;
padding: 0.5rem 1rem;
line-height: 1rem;
min-height: 2rem;
font-size: 0.875rem;
border-radius: 1rem;
margin-bottom: 0.5rem;
word-break: break-all;
text-transform: capitalize;
}
.msg.him {
float: left;
background-color: #E91E63;
color: #fff;
border-bottom-left-radius: 0.125rem;
}
.msg.you {
float: right;
background-color: #ECEFF1;
color: #607D8B;
border-bottom-right-radius: 0.125rem;
}
.msg.load {
background-color: #F8BBD0;
border-bottom-left-radius: 0.125rem;
}
.msg > span {
font-weight: 500;
position: absolute;
}
.msg > span.partner {
color: #B0BEC5;
font-size: 0.5rem;
top: 0;
font-size: 0.675rem;
margin-top: -1rem;
}
.msg > span.time {
color: #CFD8DC;
font-size: 0.5rem;
bottom: -0.35rem;
display: none;
}
.msg:hover span.time { display: block; }
.msg.him > span { left: 0; }
.msg.you > span { right: 0; }
.sendBox {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
background: white;
}
.sendBox input {
font-family: 'Lato', sans-serif;
font-size: 0.875rem;
display: block;
width: 100%;
border: none;
padding: 0.75rem 0.75rem;
line-height: 1.25rem;
border-top: 0.125rem solid #ECEFF1;
transition: 0.5s ease-in-out;
}
input:hover,
input:focus,
input:active {
outline: none!important;
border-top: 0.125rem solid #E91E63;
}
/* LOADING MESSAGE CSS */
.load .dot {
display: inline-block;
width: 0.375rem;
height: 0.375rem;
border-radius: 0.25rem;
margin-right: 0.125rem;
background-color: rgba(255,255,255,0.75);
}
.load .dot:nth-last-child(1) {animation: loadAnim 1s .2s linear infinite;}
.load .dot:nth-last-child(2) {animation: loadAnim 1s .4s linear infinite;}
.load .dot:nth-last-child(3) {animation: loadAnim 1s .6s linear infinite;}
@keyframes loadAnim {
0 {transform: translate(0,0);}
25% {transform: translate(0,-0.25rem);}
50% {transform: translate(0,0);}
}
$(function(){
// You can add Users inside JSON users section
var _json = {
users: ["Gupi Gain", "Bagha Bain", "Hirak Raja"],
chats: [{
from: 'Gupi Gain',
msg: 'Mora Dujonai Rajar Jamai!',
time: '1533263925814',
action: ''
}, {
from: 'Bagha Bain',
msg: 'Jamai!',
time: '1533263925814',
action: ''
}, {
from: 'Gupi Gain',
msg: 'Mora Khai Dai Ghuri Firi!',
time: '1533263925814',
action: ''
},{
from: 'Gupi Gain',
msg: 'Aha ki Moder Chiri!',
time: '1533263925814',
action: ''
}]
};
init();
function init () {
renderData();
};
// RENDER METHODS
function renderData () {
var _now = $.now();
getDateTime(_now);
_json.users.forEach(function (user) {
var userID = user.replace(/ /g,"_");
var parentString = '<div class="chatbox" id="'+userID+'">'+
'<div class="chats">'+
'<ul></ul>'+
'</div>'+
'<div class="sendBox">'+
'<input type="text" placeholder="enter next line '+ user.split(' ')[0]+'...">'+
'</div>';
$('#viewport').append(parentString);
_json.chats.forEach(function (chat) {
var _cl;
(chat.from === user) ? _cl = 'you' : _cl = 'him';
var dataString = '<li>'+
'<div class="msg ' + _cl +'">'+
'<span class="partner">'+ chat.from +'</span>'+
chat.msg +
'<span class="time">' + getDateTime (chat.time) + '</span>'+
'</div></li>';
$('#viewport #'+ userID +' .chats>ul').append(dataString);
});
});
};
function newMsgRender (data) {
$('#viewport .chats ul>li.pending').remove();
_json.users.forEach(function (user) {
var checkID = user.replace(/ /g,"_");
var _cl = '';
(data.from === user) ? _cl = 'you' : _cl= 'him';
$('#viewport .chatbox#'+ checkID +' .chats ul')
.append('<li><div class="msg '+_cl+'">'+
'<span class="partner">'+ data.from +'</span>'+
data.msg +
'<span class="time">' + getDateTime (data.time) + '</span>'+
'</div>'+
'</li>');
});
}
function pendingRender (typingUser) {
var pending = '<li class="pending">'+
'<div class="msg load">'+
'<div class="dot"></div>'+
'<div class="dot"></div>'+
'<div class="dot"></div>'+
'</div>'+
'</li>';
_json.users.forEach( function (user) {
user = user.replace(/ /g,"_");
if(user !== typingUser) {
if(!($('#'+ user +' .chats ul>li').hasClass('pending')))
$('#'+ user +' .chats ul').append(pending);
}
});
}
// HELPER FUNCTION
function getDateTime (t) {
var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var d = new Date(t/1000),
month = (month[d.getMonth()]),
day = d.getDate().toString(),
hour = d.getHours().toString(),
min = d.getMinutes().toString();
(day.length < 2) ? day = '0' + day : '';
(hour.length < 2) ? hour = '0' + hour : '';
(min.length < 2) ? min = '0' + min : '';
var res = ''+month+' '+day+' '+hour+ ':' + min;
return res;
}
// KEYPRESS EVENTS HANDLER
$('#viewport .sendBox>input').keypress(function( e ) {
var _id = $(this).closest('.chatbox').attr('id');
pendingRender(_id);
if(e.which == 13) {
var msgFrom;
_json.users.forEach(function (user) {
if(user.replace(/ /g,"_") === _id)
msgFrom = user;
});
var msg = $('#'+_id+' .sendBox>input').val();
msg = msg.replace(/\"/g,'\\"');
var t = $.now();
$('#'+_id+' .sendBox>input').val('');
if(msg.replace(/\s/g, '') !== ''){
var temp = {
from: msgFrom,
msg: msg,
time: t.toString(),
action: ''
}
_json.chats.push(temp);
console.log(_json);
newMsgRender (temp);
} else {
$('#viewport .chats ul>li.pending').remove();
}
}
});
// EVENT HANDLER
$('#viewport .sendBox>input').focusout(function() {
$('#viewport .chats ul>li.pending').remove();
});
});
This Pen doesn't use any external CSS resources.