refactor(slash-commands): 优化消息定位和滚动效果

b39b7998ce5cfed8e5945d0bd29e3851eb9a205a

awaae001 <3271436144@qq.com>

1 files changed, +12 -7Ignore whitespace
public/scripts/slash-commands.js+12 -7
@@ -2161,14 +2161,22 @@ export function initDefaultSlashCommands() {
21612161 // --- End of loading step ---
21622162
21632163 const messageElement = document.querySelector(`[mesid="${floorIndex}"]`);
2164+ const chatContainer = document.getElementById('chat');
21642165
21652166 if (messageElement && chatContainer) {
2166- // --- Corrected: Use the actual class from the template ---
21672167 const headerElement = messageElement.querySelector('.ch_name');
21682168 const elementToScroll = headerElement || messageElement; // Fallback to the entire message div
2169- const blockPosition = headerElement ? 'center' : 'start';
21702169
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+ });
21722180
21732181 // Highlight the message element
21742182 if (messageElement instanceof HTMLElement) {
@@ -2183,12 +2191,9 @@ export function initDefaultSlashCommands() {
21832191 messageElement.style.backgroundColor = ''; // Remove highlight
21842192 }, 1500); // Match flash duration
21852193 }
2186-
21872194 } else {
21882195 console.warn('Message element is not an HTMLElement, cannot flash highlight.');
21892196 }
2190-
2191-
21922197 } else {
21932198 // Only warn if element is not found *after* attempting to load all messages
21942199 toastr.warning(`Could not find element for message ${floorIndex} (using [mesid="${floorIndex}"]) even after attempting to load all messages. It might not be rendered yet or the index is invalid.`);