-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
66 lines (57 loc) · 1.6 KB
/
chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const chatBox = document.querySelector(".chat");
const renderBlock = document.querySelector(".chat-wrap");
import chat from "./chatlist.js"
const template = (type = 0, msg = "") => {
if (type === 0) {
return `
<div class="chat-wrap__box me">
<div class="me__text chat-wrap__text">
${msg}
</div>
</div>
`
} else {
return `
<div class="chat-wrap__box interlocutor">
<div class="interlocutor__avatar chat-wrap__avatar">
<img src="${chat.interlocutorAvatar}">
</div>
<div class="interlocutor__text chat-wrap__text">
${msg}
</div>
</div>
`
}
}
chatBox.addEventListener("scroll", function () {
document.querySelectorAll(".chat-wrap__box").forEach(box => {
if(box.offsetTop - 100 < chatBox.scrollTop){
box.classList.add("chat-wrap__box--overflow")
} else {
box.classList.remove("chat-wrap__box--overflow")
}
})
});
function randomInteger(min, max) {
let rand = min - 0.5 + Math.random() * (max - min + 1);
return Math.round(rand);
}
let delay = 100;
let focus = false;
chatBox.addEventListener("mouseenter", function () {
focus = true;
});
chatBox.addEventListener("mouseleave", function () {
focus = false;
});
chat.history.forEach((msg, idx) => {
if (idx > 4) {
delay = 1000;
}
setTimeout(() => {
renderBlock.insertAdjacentHTML("beforeEnd", template(msg.who, msg.msg));
if(!focus){
chatBox.scrollTop = chatBox.scrollHeight;
}
}, delay * idx + randomInteger(0, 0.9));
})