Advertisement
Igor150195

divs

Feb 27th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 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("div"); // Заменяем тег <a> на <div>
  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 div'); // Изменим селектор для div
  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. history.replaceState(null, null, '#' + targetId); // Устанавливаем хеш
  59. });
  60. });
  61.  
  62. titleElements.forEach(function (title) {
  63. title.addEventListener('click', function () {
  64. var targetId = this.getAttribute('id');
  65. setActiveItem(targetId);
  66. });
  67. });
  68.  
  69. const observer = new IntersectionObserver(entries => {
  70. entries.forEach(entry => {
  71. const id = entry.target.getAttribute("id");
  72. if (entry.isIntersecting) {
  73. document.querySelectorAll(".active").forEach((z) => {
  74. z.classList.remove("active");
  75. });
  76. document.querySelector(`div[data-href="#${id}"]`).classList.add("active"); // Обновляем для data-href
  77. }
  78. });
  79. }, { rootMargin: '0px 0px -50% 0px' });
  80.  
  81. if ("h2" !== "") {
  82. document.getElementById("single-article").querySelectorAll("h2").forEach(function (heading) {
  83. observer.observe(heading);
  84. });
  85. }
  86.  
  87. // handle anchor position
  88. function offsetAnchor() {
  89. if (location.hash.length !== 0) {
  90. const targetId = location.hash.substring(1);
  91. const targetElement = document.getElementById(targetId);
  92. if (targetElement) {
  93. const offset = targetElement.getBoundingClientRect().top - 100;
  94. window.scrollTo({
  95. top: window.scrollY + offset,
  96. behavior: 'smooth' // Добавляем плавный скроллинг
  97. });
  98. }
  99. }
  100. }
  101.  
  102. window.addEventListener("hashchange", offsetAnchor);
  103. window.setTimeout(offsetAnchor, 1);
  104. });
  105. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement