-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbox.html
297 lines (253 loc) · 8.7 KB
/
chatbox.html
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>仿 GPT 对话交互</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
transition: background-color 0.3s ease;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.chat-box {
height: 400px;
overflow-y: auto;
border: 1px solid #ccc;
padding: 10px;
display: flex;
flex-direction: column;
/* 设置整体滚动条样式 */
scrollbar-width: thin;
scrollbar-color: #888 #f1f1f1;
}
.chat-box::-webkit-scrollbar {
width: 6px; /* 调小整体滚动条宽度 */
}
.chat-box::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 10px;
}
.chat-box::-webkit-scrollbar-thumb {
background: #888;
border-radius: 10px;
}
.message {
margin-bottom: 10px;
padding: 8px 12px;
border-radius: 8px;
max-width: 70%;
display: inline-block;
word-wrap: break-word;
}
.user-message {
background-color: #f2f2f2;
border: 1px solid #ccc;
align-self: flex-end;
}
.bot-message {
background-color: #e8eaf6;
border: 1px solid #ccc;
align-self: flex-start;
position: relative;
}
.input-box {
display: flex;
margin-top: 10px;
}
input[type="text"] {
flex-grow: 1;
padding: 5px;
border: 1px solid #ccc;
}
button {
padding: 5px 10px;
border: none;
background-color: #4CAF50;
color: white;
cursor: pointer;
}
pre code.java {
/* 设置代码块滚动条样式 */
scrollbar-width: thin;
scrollbar-color: #666 #444;
}
pre code.java::-webkit-scrollbar {
width: 4px; /* 进一步调小代码块滚动条宽度 */
}
pre code.java::-webkit-scrollbar-track {
background: #e8eaf6; /* 白天模式下代码块滚动条轨道颜色 */
border-radius: 10px;
}
pre code.java::-webkit-scrollbar-thumb {
background: #666;
border-radius: 10px;
}
/* 夜间模式样式 */
body.night-mode {
background-color: #333;
color: #fff;
}
body.night-mode.chat-box {
border-color: #666;
}
body.night-mode input[type="text"] {
background-color: #444;
color: #fff;
border-color: #666;
}
body.night-mode button {
background-color: #008CBA;
}
body.night-mode.message {
background-color: #444;
color: #fff;
border-color: #666;
}
body.night-mode pre code.java {
background-color: #444;
color: #fff;
/* 设置代码块滚动条样式 */
scrollbar-width: thin;
scrollbar-color: #666 #444;
}
body.night-mode pre code.java::-webkit-scrollbar {
width: 4px; /* 进一步调小代码块滚动条宽度 */
}
body.night-mode pre code.java::-webkit-scrollbar-track {
background: #444;
border-radius: 10px;
}
body.night-mode pre code.java::-webkit-scrollbar-thumb {
background: #666;
border-radius: 10px;
}
.code-div {
position: relative;
}
.copy-button {
position: absolute;
top: 5px;
right: 5px;
cursor: pointer;
background: none;
border: none;
padding: 0;
}
pre:hover.copy-button {
display: inline-block;
}
</style>
</head>
<body>
<div class="container">
<div class="chat-box"></div>
<div class="input-box">
<input type="text" placeholder="输入你的问题...">
<button>发送</button>
</div>
<label class="switch">
<input type="checkbox" id="night-mode-toggle">
<span class="slider round"></span>
</label>
</div>
<script>
// 切换夜间模式
document.getElementById('night-mode-toggle').addEventListener('change', function () {
document.body.classList.toggle('night-mode');
});
// 发送消息
document.querySelector('button').addEventListener('click', function () {
const input = document.querySelector('input[type="text"]');
const message = input.value;
if (message.trim()!== '') {
const chatBox = document.querySelector('.chat-box');
const userMessageDiv = document.createElement('div');
userMessageDiv.classList.add('message', 'user-message');
userMessageDiv.textContent = '你:' + message;
chatBox.appendChild(userMessageDiv);
input.value = '';
// 模拟回复 Java 代码,逐个字符输出
const botMessageDiv = document.createElement('div');
botMessageDiv.classList.add('message', 'bot-message');
let replyText = '回复:';
let index = 0;
const intervalId = setInterval(() => {
if (index < replyText.length) {
botMessageDiv.textContent += replyText[index];
index++;
} else {
clearInterval(intervalId);
// start code block
const codeDiv = document.createElement('div');
codeDiv.classList.add('code-div');
const codeBlock = document.createElement('pre');
const code = document.createElement('code');
code.classList.add('java');
codeBlock.appendChild(code);
codeDiv.appendChild(codeBlock);
botMessageDiv.appendChild(codeDiv);
chatBox.appendChild(botMessageDiv);
const codeContent = `public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}`;
let codeIndex = 0;
const codeIntervalId = setInterval(() => {
if (codeIndex < codeContent.length) {
code.textContent += codeContent[codeIndex];
codeIndex++;
hljs.highlightAll();
// 自动滚动到最后一行
chatBox.scrollTop = chatBox.scrollHeight;
} else {
clearInterval(codeIntervalId);
// 添加复制按钮
const copyButton = document.createElement('button');
copyButton.classList.add('copy-button');
copyButton.title = '复制';
copyButton.innerHTML = '📋';
codeDiv.appendChild(copyButton);
}
}, 10);
// end code block
}
}, 100);
chatBox.appendChild(botMessageDiv);
}
});
document.addEventListener('click', function (event) {
if (event.target.classList.contains('copy-button')) {
const codeBlock = event.target.parentNode;
const codeText = codeBlock.querySelector('code').textContent;
const tempInput = document.createElement('textarea');
tempInput.value = codeText;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
event.target.innerHTML = '✔';
}
});
// 初始化代码高亮
document.addEventListener('DOMContentLoaded', function () {
hljs.highlightAll();
});
// 监听 chat-box 的变化,实现自动滚动
const chatBox = document.querySelector('.chat-box');
const observer = new MutationObserver(() => {
chatBox.scrollTop = chatBox.scrollHeight;
});
observer.observe(chatBox, { childList: true });
</script>
</body>
</html>