| 2161 | 2161 | // --- End of loading step --- |
| 2162 | 2162 | |
| 2163 | 2163 | const messageElement = document.querySelector(`[mesid="${floorIndex}"]`); |
| 2164 | + const chatContainer = document.getElementById('chat'); |
| 2164 | 2165 | |
| 2165 | 2166 | if (messageElement && chatContainer) { |
| 2166 | | - // --- Corrected: Use the actual class from the template --- |
| 2167 | 2167 | const headerElement = messageElement.querySelector('.ch_name'); |
| 2168 | 2168 | const elementToScroll = headerElement || messageElement; // Fallback to the entire message div |
| 2169 | | - const blockPosition = headerElement ? 'center' : 'start'; |
| 2170 | 2169 | |
| 2171 | | - elementToScroll.scrollIntoView({ behavior: 'smooth', block: blockPosition }); |
| 2170 | + // Calculate position relative to chat container |
| 2171 | + const elementRect = elementToScroll.getBoundingClientRect(); |
| 2172 | + const containerRect = chatContainer.getBoundingClientRect(); |
| 2173 | + const scrollPosition = elementRect.top - containerRect.top + chatContainer.scrollTop; |
| 2174 | + |
| 2175 | + const viewportOffset = chatContainer.clientHeight * 0.1; // 25% from top |
| 2176 | + chatContainer.scrollTo({ |
| 2177 | + top: scrollPosition - viewportOffset, |
| 2178 | + behavior: 'smooth', |
| 2179 | + }); |
| 2172 | 2180 | |
| 2173 | 2181 | // Highlight the message element |
| 2174 | 2182 | if (messageElement instanceof HTMLElement) { |