|
2 | 2 | <html lang="zh-CN"> |
3 | 3 | <head> |
4 | 4 | <meta charset="UTF-8"> |
5 | | - <title>渐变聊天室</title> |
| 5 | + <title>多用户聊天室</title> |
6 | 6 | <style> |
| 7 | + :root { |
| 8 | + --primary-color: #4ecdc4; |
| 9 | + --secondary-color: #f9d423; |
| 10 | + } |
| 11 | + |
7 | 12 | * { |
8 | 13 | margin: 0; |
9 | 14 | padding: 0; |
10 | 15 | box-sizing: border-box; |
11 | | - font-family: 'Microsoft YaHei', sans-serif; |
| 16 | + font-family: 'Segoe UI', sans-serif; |
12 | 17 | } |
13 | 18 |
|
14 | 19 | body { |
15 | | - background: linear-gradient(135deg, #f9d423 0%, #4ecdc4 100%); |
| 20 | + background: linear-gradient(135deg, var(--secondary-color), var(--primary-color)); |
16 | 21 | min-height: 100vh; |
| 22 | + padding: 20px; |
17 | 23 | } |
18 | 24 |
|
19 | 25 | .chat-container { |
20 | 26 | max-width: 800px; |
21 | | - margin: 20px auto; |
| 27 | + margin: 0 auto; |
22 | 28 | background: rgba(255, 255, 255, 0.9); |
23 | 29 | border-radius: 15px; |
24 | 30 | box-shadow: 0 0 20px rgba(0, 0, 0, 0.2); |
25 | | - overflow: hidden; |
26 | 31 | height: 90vh; |
27 | 32 | display: flex; |
28 | 33 | flex-direction: column; |
|
32 | 37 | flex: 1; |
33 | 38 | padding: 20px; |
34 | 39 | overflow-y: auto; |
| 40 | + display: flex; |
| 41 | + flex-direction: column; |
35 | 42 | } |
36 | 43 |
|
37 | 44 | .message { |
38 | 45 | max-width: 70%; |
39 | | - margin-bottom: 15px; |
40 | | - padding: 12px 18px; |
41 | | - border-radius: 20px; |
42 | | - animation: fadeIn 0.3s ease; |
43 | | - word-break: break-word; |
| 46 | + margin: 10px; |
| 47 | + padding: 15px; |
| 48 | + border-radius: 15px; |
| 49 | + animation: slideIn 0.3s ease; |
| 50 | + position: relative; |
| 51 | + } |
| 52 | + |
| 53 | + .user-message { |
| 54 | + background: var(--primary-color); |
| 55 | + color: white; |
| 56 | + align-self: flex-end; |
44 | 57 | } |
45 | 58 |
|
46 | | - .received { |
| 59 | + .other-message { |
47 | 60 | background: #f1f1f1; |
48 | 61 | align-self: flex-start; |
49 | 62 | } |
50 | 63 |
|
51 | | - .sent { |
52 | | - background: #4ecdc4; |
53 | | - color: white; |
54 | | - align-self: flex-end; |
| 64 | + .message-info { |
| 65 | + display: flex; |
| 66 | + justify-content: space-between; |
| 67 | + font-size: 0.8em; |
| 68 | + margin-bottom: 5px; |
55 | 69 | } |
56 | 70 |
|
57 | 71 | .input-area { |
58 | 72 | padding: 20px; |
59 | 73 | background: rgba(255, 255, 255, 0.95); |
60 | | - border-top: 1px solid #eee; |
| 74 | + border-top: 1px solid #ddd; |
61 | 75 | display: flex; |
62 | 76 | gap: 10px; |
63 | 77 | } |
64 | 78 |
|
65 | | - input { |
66 | | - flex: 1; |
| 79 | + input, select { |
67 | 80 | padding: 12px; |
68 | | - border: 2px solid #4ecdc4; |
| 81 | + border: 2px solid var(--primary-color); |
69 | 82 | border-radius: 25px; |
70 | 83 | outline: none; |
71 | | - font-size: 16px; |
72 | 84 | } |
73 | 85 |
|
74 | 86 | button { |
75 | 87 | padding: 12px 25px; |
76 | | - background: #4ecdc4; |
| 88 | + background: var(--primary-color); |
77 | 89 | border: none; |
78 | 90 | border-radius: 25px; |
79 | 91 | color: white; |
80 | 92 | cursor: pointer; |
81 | | - transition: all 0.3s ease; |
| 93 | + transition: 0.3s; |
82 | 94 | } |
83 | 95 |
|
84 | 96 | button:hover { |
85 | 97 | background: #3db3ab; |
86 | 98 | transform: translateY(-2px); |
87 | 99 | } |
88 | 100 |
|
89 | | - @keyframes fadeIn { |
| 101 | + @keyframes slideIn { |
90 | 102 | from { |
91 | 103 | opacity: 0; |
92 | | - transform: translateY(10px); |
| 104 | + transform: translateY(20px); |
93 | 105 | } |
94 | 106 | to { |
95 | 107 | opacity: 1; |
96 | 108 | transform: translateY(0); |
97 | 109 | } |
98 | 110 | } |
99 | | - |
100 | | - /* 滚动条样式 */ |
101 | | - ::-webkit-scrollbar { |
102 | | - width: 8px; |
103 | | - } |
104 | | - |
105 | | - ::-webkit-scrollbar-track { |
106 | | - background: rgba(0, 0, 0, 0.1); |
107 | | - } |
108 | | - |
109 | | - ::-webkit-scrollbar-thumb { |
110 | | - background: #4ecdc4; |
111 | | - border-radius: 4px; |
112 | | - } |
113 | 111 | </style> |
114 | 112 | </head> |
115 | 113 | <body> |
116 | 114 | <div class="chat-container"> |
117 | | - <div class="messages" id="messages"> |
118 | | - <!-- 示例消息 --> |
119 | | - <div class="message received">你好!有什么可以帮你的?</div> |
120 | | - <div class="message sent">你好!这个界面真漂亮</div> |
121 | | - </div> |
| 115 | + <div class="messages" id="messages"></div> |
122 | 116 | <div class="input-area"> |
123 | | - <input type="text" id="messageInput" placeholder="输入消息..." /> |
| 117 | + <select id="userSelect"> |
| 118 | + <option value="user1">用户1</option> |
| 119 | + <option value="user2">用户2</option> |
| 120 | + </select> |
| 121 | + <input type="text" id="messageInput" placeholder="输入消息..."> |
124 | 122 | <button onclick="sendMessage()">发送</button> |
125 | 123 | </div> |
126 | 124 | </div> |
127 | 125 |
|
128 | 126 | <script> |
129 | 127 | const messagesContainer = document.getElementById('messages'); |
130 | 128 | const messageInput = document.getElementById('messageInput'); |
| 129 | + const userSelect = document.getElementById('userSelect'); |
| 130 | + |
| 131 | + // 初始化消息存储 |
| 132 | + let messages = JSON.parse(localStorage.getItem('chatMessages')) || []; |
131 | 133 |
|
132 | | - // 发送消息函数 |
133 | | - function sendMessage() { |
134 | | - const message = messageInput.value.trim(); |
135 | | - if (message) { |
136 | | - // 添加发送的消息 |
137 | | - addMessage(message, 'sent'); |
138 | | - // 清空输入框 |
139 | | - messageInput.value = ''; |
140 | | - // 模拟回复 |
141 | | - setTimeout(() => { |
142 | | - addMessage('已收到你的消息:' + message, 'received'); |
143 | | - }, 1000); |
144 | | - } |
| 134 | + // 加载历史消息 |
| 135 | + function loadMessages() { |
| 136 | + messagesContainer.innerHTML = ''; |
| 137 | + messages.forEach(msg => { |
| 138 | + createMessageElement(msg); |
| 139 | + }); |
145 | 140 | } |
146 | 141 |
|
147 | | - // 添加消息到界面 |
148 | | - function addMessage(text, type) { |
| 142 | + // 创建消息元素 |
| 143 | + function createMessageElement(msg) { |
149 | 144 | const messageDiv = document.createElement('div'); |
150 | | - messageDiv.className = `message ${type}`; |
151 | | - messageDiv.textContent = text; |
| 145 | + messageDiv.className = `message ${msg.sender === 'user1' ? 'user-message' : 'other-message'}`; |
| 146 | + |
| 147 | + const time = new Date(msg.timestamp).toLocaleTimeString(); |
| 148 | + messageDiv.innerHTML = ` |
| 149 | + <div class="message-info"> |
| 150 | + <span>${msg.sender}</span> |
| 151 | + <span>${time}</span> |
| 152 | + </div> |
| 153 | + <div>${msg.content}</div> |
| 154 | + `; |
| 155 | + |
152 | 156 | messagesContainer.appendChild(messageDiv); |
153 | | - // 自动滚动到底部 |
154 | 157 | messagesContainer.scrollTop = messagesContainer.scrollHeight; |
155 | 158 | } |
156 | 159 |
|
157 | | - // 回车发送功能 |
158 | | - messageInput.addEventListener('keypress', (e) => { |
159 | | - if (e.key === 'Enter') { |
160 | | - sendMessage(); |
| 160 | + // 发送消息 |
| 161 | + function sendMessage() { |
| 162 | + const content = messageInput.value.trim(); |
| 163 | + if (content) { |
| 164 | + const message = { |
| 165 | + sender: userSelect.value, |
| 166 | + content: content, |
| 167 | + timestamp: new Date().getTime() |
| 168 | + }; |
| 169 | + |
| 170 | + messages.push(message); |
| 171 | + localStorage.setItem('chatMessages', JSON.stringify(messages)); |
| 172 | + createMessageElement(message); |
| 173 | + messageInput.value = ''; |
161 | 174 | } |
| 175 | + } |
| 176 | + |
| 177 | + // 初始化加载消息 |
| 178 | + loadMessages(); |
| 179 | + |
| 180 | + // 回车发送 |
| 181 | + messageInput.addEventListener('keypress', (e) => { |
| 182 | + if (e.key === 'Enter') sendMessage(); |
162 | 183 | }); |
163 | 184 | </script> |
164 | 185 | </body> |
|
0 commit comments