본문 바로가기
유틸리티

티스토리 목차 적용하기 (TOC)

by goatlab 2023. 6. 15.
728x90
반응형
SMALL

html

 

블로그 관리 스킨 편집 html 편집으로 이동한다. head 태그 밑에 다음 코드를 추가한다.

 

<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.11.1/tocbot.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.11.1/tocbot.css">

 

<s_permalink_article_rep> 위치를 검색하여 다음 코드를 추가한다.

 

<div class='toc'></div>

 

html 편집기 맨 밑에 다음 스크립트 코드를 추가한다.

 

<script>
  var content = document.querySelector('.entry-content')
  var headings = content.querySelectorAll('h1, h2, h3, h4, h5, h6, h7')
  var headingMap = {}

  Array.prototype.forEach.call(headings, function (heading) {
      var id = heading.id ? heading.id : heading.textContent.trim().toLowerCase()
                 .split(' ').join('-').replace(/[\!\@\#\$\%\^\&\*\(\):]/ig, '')
      headingMap[id] = !isNaN(headingMap[id]) ? ++headingMap[id] : 0
      if (headingMap[id]) {
        heading.id = id + '-' + headingMap[id]
      } else {
        heading.id = id
      }
    })

  tocbot.init({
    tocSelector: '.toc',
    contentSelector: '.entry-content',
    headingSelector:'h1, h2, h3',
    hasInnerContainers: false
  });

  $(document).ready(function(){
    $('.toc').addClass('toc-absolute');

    var toc_top = $('.toc').offset().top - 165;
    $(window).scroll(function() {
      if ($(this).scrollTop() >= toc_top) {
        $('.toc').addClass('toc-fixed');
        $('.toc').removeClass('toc-absolute');
      } else {
        $('.toc').addClass('toc-absolute');
        $('.toc').removeClass('toc-fixed');
      }
    });
  });

</script>

 

css

 

css로 이동하여 다음 코드를 추가하여 스타일을 적용한다.

 

.toc-absolute {
  position: absolute;
  margin-top:165px;
}
.toc-fixed {
  position: fixed;
  top: 165px;
}

.toc {
  right: 8%;
  width: 250px;
  padding: 10px;
  box-sizing: border-box;
}

.toc-list {
  margin-top: 10px !important;
  font-size: 0.9em;
}

.toc > .toc-list li {
  margin-bottom: 10px;
}

.toc > .toc-list li:last-child {
  margin-bottom: 0;
}

.toc > .toc-list li a {
  text-decoration: none;
}

TOC 적용된 화면

728x90
반응형
LIST

'유틸리티' 카테고리의 다른 글

한글 파일-워드 변환 (hwp-to-word)  (0) 2022.08.24
RGB Colors  (0) 2022.08.12
[참고 문헌 작성법] APA Style  (0) 2022.08.10
[참고 문헌 작성법] MLA Style  (0) 2022.08.10
[참고 문헌 작성법] Chicago Manual  (0) 2022.08.10