목록 저튼 상단 고정 h1 태그만 가능 소데목 코드 짜여있으면 적용 불가
<!-- 목록 빨간색 깜빡이 전브다 같은거 -->
<style>
/* 상단 버튼 컨테이너 (화면 따라다니기 해제) */
#toc-container {
display: flex;
flex-wrap: wrap;
gap: 8px;
padding: 12px;
background-color: #222;
box-shadow: 0 4px 6px rgba(0,0,0,0.2);
margin-bottom: 20px;
}
/* 깜빡이는 버튼 스타일 */
.blinking-button {
background-color: red;
color: yellow;
padding: 8px 16px;
font-size: 15px;
font-weight: bold;
border: none;
border-radius: 8px;
cursor: pointer;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
animation: blink 1s infinite alternate;
}
@keyframes blink {
0% { opacity: 1; }
100% { opacity: 0.3; }
}
.blinking-button:hover {
opacity: 0.8;
}
/* h1 여백 제거 및 배경 전환 효과 유지 */
h1 {
transition: background-color 0.3s ease;
}
/* 이동 후 도착한 제목에서 반짝이는 불빛 효과 */
.target-highlight {
animation: flashGlow 1.5s ease-in-out;
}
@keyframes flashGlow {
0% { background-color: #ffea00; box-shadow: 0 0 20px #ffea00; color: #000; }
25% { background-color: transparent; box-shadow: none; }
50% { background-color: #ffea00; box-shadow: 0 0 20px #ffea00; color: #000; }
75% { background-color: transparent; box-shadow: none; }
100% { background-color: #ffea00; box-shadow: 0 0 20px #ffea00; color: #000; }
}
</style>
<div id="toc-container"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const tocContainer = document.getElementById('toc-container');
// 1. 블로그 본문 내부의 H1만 추출 (상단 블로그 제목/헤더 영역의 H1은 자동 제외)
let headings = document.querySelectorAll('.post-body h1, .entry-content h1, article h1, main h1');
// 만약 위 클래스가 없는 일반 페이지일 경우 헤더를 제외한 모든 H1 선택
if (headings.length === 0) {
headings = Array.from(document.querySelectorAll('h1')).filter(h1 => !h1.closest('header, nav, #toc-container'));
}
headings.forEach(function(heading, index) {
// 이동할 앵커 ID 자동 부여
if (!heading.id) {
heading.id = 'toc-h1-' + index;
}
// 버튼 생성
const button = document.createElement('button');
button.className = 'blinking-button';
button.textContent = '👀 ' + heading.textContent.trim() + ' 👈';
// 버튼 클릭 시 이벤트 (조금 아래로 이동하도록 수정)
button.addEventListener('click', function() {
// 💡 핵심 수정 부분: 화면 높이의 약 25%(1/4)만큼 여백을 두고 스크롤 이동
const offset = window.innerHeight * 0.25; // 0.25를 늘리면 더 아래로, 줄이면 더 위로 갑니다.
const elementPosition = heading.getBoundingClientRect().top + window.scrollY;
window.scrollTo({
top: elementPosition - offset,
behavior: 'smooth'
});
// 도착한 위치의 H1 제목에 불빛 반짝임 애니메이션 적용
heading.classList.remove('target-highlight');
void heading.offsetWidth; // 애니메이션 리셋(재실행 가능하게 처리)
heading.classList.add('target-highlight');
// 애니메이션 종료 후 클래스 제거
setTimeout(function() {
heading.classList.remove('target-highlight');
}, 1500);
});
tocContainer.appendChild(button);
});
});
</script>
<!-- 목록 빨간색 깜빡이 전브다 같은거 -->