diff options
| author | Josh <[email protected]> | 2020-05-27 14:56:38 +1000 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2020-12-18 21:18:50 -0500 |
| commit | 70c73084b38c8788e6fda907c031f6eec2d13847 (patch) | |
| tree | b13f92ff9812f023f4250be161222e71c64fb485 /docs/_static/custom.js | |
| parent | Add favicon (diff) | |
| download | discord.py-70c73084b38c8788e6fda907c031f6eec2d13847.tar.xz discord.py-70c73084b38c8788e6fda907c031f6eec2d13847.zip | |
[matrix] Refactor docs JS
* Refactor custom.js
* Refactor scorer.js
* tables variable shoudn't be in global scope
Diffstat (limited to 'docs/_static/custom.js')
| -rw-r--r-- | docs/_static/custom.js | 63 |
1 files changed, 33 insertions, 30 deletions
diff --git a/docs/_static/custom.js b/docs/_static/custom.js index 7564d72b..b8bac63b 100644 --- a/docs/_static/custom.js +++ b/docs/_static/custom.js @@ -1,34 +1,11 @@ -$(document).ready(function () { - var sections = $('div.section'); - var activeLink = null; - var bottomHeightThreshold = $(document).height() - 30; - - $(window).scroll(function (event) { - var distanceFromTop = $(this).scrollTop(); - var currentSection = null; - - if(distanceFromTop + window.innerHeight > bottomHeightThreshold) { - currentSection = $(sections[sections.length - 1]); - } - else { - sections.each(function () { - var section = $(this); - if (section.offset().top - 1 < distanceFromTop) { - currentSection = section; - } - }); - } - - if (activeLink) { - activeLink.parent().removeClass('active'); - } - - if (currentSection) { - activeLink = $('.sphinxsidebar a[href="#' + currentSection.attr('id') + '"]'); - activeLink.parent().addClass('active'); - } +'use-strict'; - }); +let activeLink = null; +let bottomHeightThreshold, sections; + +document.addEventListener('DOMContentLoaded', () => { + bottomHeightThreshold = document.documentElement.scrollHeight - 30; + sections = document.querySelectorAll('div.section'); const tables = document.querySelectorAll('.py-attribute-table[data-move-to-id]'); tables.forEach(table => { @@ -38,3 +15,29 @@ $(document).ready(function () { parent.insertBefore(table, element.nextSibling); }); }); + +window.addEventListener('scroll', () => { + let currentSection = null; + + if (window.scrollY + window.innerHeight > bottomHeightThreshold) { + currentSection = sections[sections.length - 1]; + } + else { + sections.forEach(section => { + let rect = section.getBoundingClientRect(); + if (rect.top + document.body.scrollTop - 1 < window.scrollY) { + currentSection = section; + } + }); + } + + if (activeLink) { + activeLink.parentElement.classList.remove('active'); + } + + if (currentSection) { + activeLink = document.querySelector(`.sphinxsidebar a[href="#${currentSection.id}"]`); + activeLink.parentElement.classList.add('active'); + } + +});
\ No newline at end of file |