Advertisement
Igor150195

ssss

Feb 27th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. <script>
  2. document.addEventListener('DOMContentLoaded', function () {
  3. const article = document.getElementById("single-article");
  4. const tocContainer = document.getElementById("toc");
  5.  
  6. // Create the TOC
  7. const createTOC = () => {
  8. const headings = article.querySelectorAll("h2");
  9. const tocFragment = document.createDocumentFragment();
  10.  
  11. headings.forEach((heading) => {
  12. const title = heading.textContent.trim();
  13. const anchorId = `toc-${title.toLowerCase().replace(/\s+/g, '-')}`;
  14.  
  15. heading.id = anchorId;
  16.  
  17. const li = document.createElement("li");
  18. const anchor = document.createElement("a");
  19. anchor.textContent = title;
  20. anchor.dataset.href = `#${anchorId}`; // Используем data-href
  21. li.appendChild(anchor);
  22. tocFragment.appendChild(li);
  23. });
  24.  
  25. const ul = document.createElement("ul");
  26. ul.appendChild(tocFragment);
  27. tocContainer.appendChild(ul);
  28. };
  29.  
  30. // Check if the TOC container exists and the article has headings
  31. if (tocContainer && article) {
  32. createTOC();
  33. }
  34.  
  35. var tocItems = document.querySelectorAll('#toc a');
  36. var titleElements = document.querySelectorAll('.content [id]');
  37.  
  38. function setActiveItem(targetId) {
  39. tocItems.forEach(function (item) {
  40. if (item.dataset.href === `#${targetId}`) { // Сравниваем по data-href
  41. item.classList.add('active');
  42. } else {
  43. item.classList.remove('active');
  44. }
  45. });
  46. }
  47.  
  48. tocItems.forEach(function (item) {
  49. item.addEventListener('click', function (event) {
  50. event.preventDefault();
  51. var targetId = this.dataset.href.substring(1); // Извлекаем id из data-href
  52. var element = document.getElementById(targetId);
  53. const offset = element.getBoundingClientRect().top - 100;
  54. window.scrollTo({
  55. top: window.scrollY + offset,
  56. behavior: 'smooth' // Добавляем плавный скроллинг
  57. });
  58. // Устанавливаем хеш
  59. history.pushState(null, null, '#' + targetId);
  60. });
  61. });
  62.  
  63. titleElements.forEach(function (title) {
  64. title.addEventListener('click', function () {
  65. var targetId = this.getAttribute('id');
  66. setActiveItem(targetId);
  67. });
  68. });
  69.  
  70. const observer = new IntersectionObserver(entries => {
  71. entries.forEach(entry => {
  72. const id = entry.target.getAttribute("id");
  73. if (entry.isIntersecting) {
  74. document.querySelectorAll(".active").forEach((z) => {
  75. z.classList.remove("active");
  76. });
  77. document.querySelector(`a[data-href="#${id}"]`).classList.add("active"); // Обновляем для data-href
  78. }
  79. });
  80. }, { rootMargin: '0px 0px -50% 0px' });
  81.  
  82. if ("h2" !== "") {
  83. document.getElementById("single-article").querySelectorAll("h2").forEach(function (heading) {
  84. observer.observe(heading);
  85. });
  86. }
  87.  
  88. // handle anchor position
  89. function offsetAnchor() {
  90. if (location.hash.length !== 0) {
  91. const targetId = location.hash.substring(1);
  92. const targetElement = document.getElementById(targetId);
  93. if (targetElement) {
  94. const offset = targetElement.getBoundingClientRect().top - 100;
  95. window.scrollTo({
  96. top: window.scrollY + offset,
  97. behavior: 'smooth' // Добавляем плавный скроллинг
  98. });
  99. }
  100. }
  101. }
  102.  
  103. window.addEventListener("hashchange", offsetAnchor);
  104. window.setTimeout(offsetAnchor, 1);
  105. });
  106. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement