|
197 | 197 | closeSidebar(); |
198 | 198 | }); |
199 | 199 |
|
| 200 | + // Prevent body scroll when scrolling in sidebar |
| 201 | + const sidebar = document.querySelector(".sidebar"); |
| 202 | + let isScrollingSidebar = false; |
| 203 | + |
| 204 | + sidebar.addEventListener("wheel", (e) => { |
| 205 | + const sidebarElement = e.currentTarget; |
| 206 | + const { scrollTop, scrollHeight, clientHeight } = sidebarElement; |
| 207 | + |
| 208 | + // Check if we're at the top or bottom of the sidebar |
| 209 | + const isAtTop = scrollTop === 0; |
| 210 | + const isAtBottom = scrollTop + clientHeight >= scrollHeight - 1; // Small tolerance for rounding |
| 211 | + |
| 212 | + // If scrolling up and at top, or scrolling down and at bottom, prevent default |
| 213 | + if ((e.deltaY < 0 && isAtTop) || (e.deltaY > 0 && isAtBottom)) { |
| 214 | + e.preventDefault(); |
| 215 | + } |
| 216 | + }, { passive: false }); |
| 217 | + |
| 218 | + // Also handle touch events for mobile |
| 219 | + sidebar.addEventListener("touchstart", () => { |
| 220 | + isScrollingSidebar = true; |
| 221 | + }); |
| 222 | + |
| 223 | + sidebar.addEventListener("touchend", () => { |
| 224 | + isScrollingSidebar = false; |
| 225 | + }); |
| 226 | + |
| 227 | + // Prevent body scroll when actively scrolling sidebar |
| 228 | + document.addEventListener("wheel", (e) => { |
| 229 | + if (isScrollingSidebar) { |
| 230 | + e.preventDefault(); |
| 231 | + } |
| 232 | + }, { passive: false }); |
| 233 | + |
| 234 | + // Ensure sidebar content is properly scrollable when expanded |
| 235 | + const ensureSidebarScrollability = () => { |
| 236 | + const sidebar = document.querySelector(".sidebar"); |
| 237 | + if (sidebar) { |
| 238 | + // Force a reflow to ensure proper height calculation |
| 239 | + sidebar.style.height = 'auto'; |
| 240 | + sidebar.offsetHeight; // Trigger reflow |
| 241 | + sidebar.style.height = ''; |
| 242 | + } |
| 243 | + }; |
| 244 | + |
| 245 | + // Run on menu expansion |
| 246 | + document.addEventListener('click', (e) => { |
| 247 | + if (e.target.closest('.sidebar a') && e.target.closest('li').classList.contains('has-children')) { |
| 248 | + setTimeout(ensureSidebarScrollability, 100); |
| 249 | + } |
| 250 | + }); |
| 251 | + |
200 | 252 | backdrop.addEventListener("click", () => { |
201 | 253 | closeSidebar(); |
202 | 254 | }); |
|
0 commit comments