${postTitle}
`; container.appendChild(item); }); // 첫 번째 박스 로드 완료 후 두 번째 박스 위치 조정 adjustSecondBoxPosition(); }) .catch(error => { console.error('최신 글 로드 실패:', error); }); } // 두 번째 박스 위치를 동적으로 조정하는 함수 function adjustSecondBoxPosition() { const firstBox = document.getElementById('sidebar-right-container'); const secondBox = document.getElementById('news-container'); if (firstBox && secondBox) { const firstBoxRect = firstBox.getBoundingClientRect(); const firstBoxTop = firstBoxRect.top + window.scrollY; const firstBoxHeight = firstBox.offsetHeight; // 첫 번째 박스 아래에 30px 간격으로 배치 const newTop = firstBoxTop + firstBoxHeight + 30; secondBox.style.top = newTop + 'px'; } } // 광고가 있으면 이번 주 인기글을 아래로 밀기 function adjustPositionForAd() { const adContainer = document.getElementById('sidebar-ad-container'); const popularContainer = document.getElementById('sidebar-right-container'); if (popularContainer) { if (adContainer) { popularContainer.style.top = '550px'; } else { popularContainer.style.top = '350px'; } // 최신글 위치도 재조정 adjustSecondBoxPosition(); } } // 함수 실행 loadSidebarLatestPosts(); // 광고 존재 여부에 따라 위치 조정 (DOM 로드 후) setTimeout(adjustPositionForAd, 300); // ResizeObserver로 sidebar-right-container 크기 변화 감지 → 자동 위치 재조정 const sidebarBox = document.getElementById('sidebar-right-container'); if (sidebarBox && typeof ResizeObserver !== 'undefined') { const observer = new ResizeObserver(() => { adjustSecondBoxPosition(); }); observer.observe(sidebarBox); } // 페이지 완전 로드 후 최종 위치 조정 window.addEventListener('load', () => { adjustSecondBoxPosition(); }); // 윈도우 리사이즈 시 위치 재조정 window.addEventListener('resize', () => { adjustSecondBoxPosition(); }); // 스크롤 시 하단 고정 로직 function initStickyScroll() { const firstBox = document.getElementById('sidebar-right-container'); const secondBox = document.getElementById('news-container'); if (!firstBox || !secondBox) { setTimeout(initStickyScroll, 200); return; } function isMobileView() { return window.innerWidth <= 1100; } const adContainer = document.getElementById('sidebar-ad-container'); let originalFirstTop = adContainer ? 550 : 350; let originalSecondTop = null; let isSticky = false; function updateOriginalPositions() { if (!isSticky) { const firstStyle = window.getComputedStyle(firstBox); const secondStyle = window.getComputedStyle(secondBox); if (firstStyle.position === 'absolute') { originalFirstTop = parseInt(firstStyle.top) || (adContainer ? 550 : 350); } if (secondStyle.position === 'absolute') { originalSecondTop = parseInt(secondStyle.top) || (originalFirstTop + firstBox.offsetHeight + 30); } } } function resetToOriginalPosition() { if (isSticky || firstBox.classList.contains('is-fixed')) { isSticky = false; firstBox.style.position = 'absolute'; firstBox.style.top = originalFirstTop + 'px'; firstBox.style.bottom = 'auto'; firstBox.classList.remove('is-fixed'); secondBox.style.position = 'absolute'; secondBox.style.top = originalSecondTop + 'px'; secondBox.style.bottom = 'auto'; secondBox.classList.remove('is-fixed'); } } function handleScroll() { if (isMobileView()) { firstBox.classList.remove('is-fixed'); secondBox.classList.remove('is-fixed'); firstBox.style.position = ''; firstBox.style.top = ''; firstBox.style.bottom = ''; secondBox.style.position = ''; secondBox.style.top = ''; secondBox.style.bottom = ''; isSticky = false; return; } const scrollY = window.scrollY || window.pageYOffset; const viewportHeight = window.innerHeight; const firstBoxHeight = firstBox.offsetHeight; const secondBoxHeight = secondBox.offsetHeight; const gap = 30; const bottomPadding = 20; if (originalSecondTop === null) { updateOriginalPositions(); } const secondBoxBottom = originalSecondTop + secondBoxHeight; const triggerPoint = secondBoxBottom - viewportHeight + bottomPadding; const minScrollThreshold = 100; if (scrollY < minScrollThreshold) { resetToOriginalPosition(); return; } if (scrollY > triggerPoint && triggerPoint > 0) { if (!isSticky) { isSticky = true; firstBox.style.position = 'fixed'; firstBox.style.top = 'auto'; firstBox.style.bottom = (secondBoxHeight + gap + bottomPadding) + 'px'; firstBox.classList.add('is-fixed'); secondBox.style.position = 'fixed'; secondBox.style.top = 'auto'; secondBox.style.bottom = bottomPadding + 'px'; secondBox.classList.add('is-fixed'); } } else { resetToOriginalPosition(); } } setTimeout(() => { updateOriginalPositions(); resetToOriginalPosition(); window.addEventListener('scroll', handleScroll); window.addEventListener('resize', () => { // 리사이즈 시 강제 초기화 및 재계산 if (isSticky) resetToOriginalPosition(); if (typeof adjustSecondBoxPosition === 'function') adjustSecondBoxPosition(); updateOriginalPositions(); handleScroll(); }); }, 600); } // 스크롤 고정 초기화 (두 번째 박스 로드 후) setTimeout(initStickyScroll, 800); }); ]]>${postTitle}
`; container.appendChild(item); }); } fetch('/feeds/posts/default?alt=json&max-results=5') .then(res => res.json()) .then(data => { const entries = data.feed.entry; if (!entries) return; // PC + 모바일 둘 다 채움 (CSS가 표시 제어) populateContainer('news-latest', entries); populateContainer('news-latest-mobile', entries); // PC 위치 조정 if (typeof adjustSecondBoxPosition === 'function') { adjustSecondBoxPosition(); } }) .catch(error => console.error('최신글 로드 실패:', error)); /* ============================================= PC 위치 조정 함수 ============================================= */ window.adjustSecondBoxPosition = function() { const firstBox = document.getElementById('sidebar-right-container'); const secondBox = document.getElementById('news-container'); if (firstBox && secondBox) { const firstBoxRect = firstBox.getBoundingClientRect(); const firstBoxTop = firstBoxRect.top + window.scrollY; const firstBoxHeight = firstBox.offsetHeight; const newTop = firstBoxTop + firstBoxHeight + 30; secondBox.style.top = newTop + 'px'; } }; window.addEventListener('load', () => adjustSecondBoxPosition()); window.addEventListener('resize', () => adjustSecondBoxPosition()); }); ]]>