목록 버튼 버튼이 따라오네?
<style>
/* 상단 버튼 컨테이너 */
#toc-container {
display: flex;
flex-wrap: wrap;
gap: 8px;
padding: 12px;
background-color: #222;
position: sticky;
top: 0;
z-index: 9999;
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 {
scroll-margin-top: 100px;
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() {
// 해당 위치로 스크롤
heading.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
// 도착한 위치의 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>