-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
90 lines (85 loc) · 2.76 KB
/
script.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
document.addEventListener("DOMContentLoaded", function () {
const chatContainer = document.getElementById("chat-container");
const messages = [
"Hello! How are you?",
"I'm good, thanks! And you?",
"Doing well, thanks for asking!",
"What are you up to today?",
"Just working on a project. You?",
"Same here. Just a different project.",
"Any plans for the weekend?",
"Not sure yet. Maybe a hike?",
"Sounds fun!",
"Yeah, looking forward to it.",
"Got to go now, talk later?",
"Sure, catch up soon!",
"Take care!",
"You too!",
"Bye!",
"See ya!",
"👋",
"👋",
"Have a nice day!",
"Thanks, you too!",
];
let loadedIndex = 0;
let loadedMessageScrollPosition = 0;
let lastScrollTop = 0; // Variable to store the last scroll position
const scrollThreshold = 250;
// Function to create a message element
function createMessage(text, isSelf) {
let div = document.createElement("div");
div.className = "message" + (isSelf ? " self" : " other");
if (isSelf) {
div.innerText = text;
} else {
div.innerText = "...";
setTimeout(() => {
div.innerText = text;
}, 250);
}
return div;
}
// Event listener to load more messages on scroll
window.addEventListener("scroll", function () {
let currentScroll =
window.pageYOffset || document.documentElement.scrollTop;
if (currentScroll > lastScrollTop) {
// Scrolling down
// Load more messages if needed
if (
currentScroll > lastScrollTop &&
loadedIndex < messages.length
) {
const messagesToAdd = Math.floor((currentScroll - loadedMessageScrollPosition) / scrollThreshold);
for (let i = 0; i < messagesToAdd; i++) {
let message = createMessage(
messages[loadedIndex],
loadedIndex % 2 === 0
);
chatContainer.appendChild(message);
loadedIndex++;
loadedMessageScrollPosition = currentScroll;
}
lastScrollTop = currentScroll;
}
} else {
// Scrolling up
// Hide messages that are higher than the current scroll position
const messagesToRemove = Math.floor((loadedMessageScrollPosition - currentScroll) / scrollThreshold);
if (messagesToRemove > 0) {
const messages = Array.from(chatContainer.children);
const removing = messages.slice(messages.length - messagesToRemove);
removing.forEach(m => {
chatContainer.removeChild(m);
loadedIndex--;
loadedMessageScrollPosition = currentScroll;
});
}
}
lastScrollTop = currentScroll <= 0 ? 0 : currentScroll; // Reset lastScrollTop at the top
});
window.onbeforeunload = function () {
window.scrollTo(0, 0);
};
});