[78ef36]: / docs-source / pytorch_sphinx_theme / js / highlight-navigation.js

Download this file

72 lines (56 with data), 2.6 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Modified from https://stackoverflow.com/a/32396543
window.highlightNavigation = {
navigationListItems: document.querySelectorAll("#pytorch-right-menu li"),
sections: document.querySelectorAll(".pytorch-article .section"),
sectionIdTonavigationLink: {},
bind: function() {
if (!sideMenus.displayRightMenu) {
return;
};
for (var i = 0; i < highlightNavigation.sections.length; i++) {
var id = highlightNavigation.sections[i].id;
highlightNavigation.sectionIdTonavigationLink[id] =
document.querySelectorAll('#pytorch-right-menu li a[href="#' + id + '"]')[0];
}
$(window).scroll(utilities.throttle(highlightNavigation.highlight, 100));
},
highlight: function() {
var rightMenu = document.getElementById("pytorch-right-menu");
// If right menu is not on the screen don't bother
if (rightMenu.offsetWidth === 0 && rightMenu.offsetHeight === 0) {
return;
}
var scrollPosition = utilities.scrollTop();
var OFFSET_TOP_PADDING = 25;
var offset = document.getElementById("header-holder").offsetHeight +
document.getElementById("pytorch-page-level-bar").offsetHeight +
OFFSET_TOP_PADDING;
var sections = highlightNavigation.sections;
for (var i = (sections.length - 1); i >= 0; i--) {
var currentSection = sections[i];
var sectionTop = utilities.offset(currentSection).top;
if (scrollPosition >= sectionTop - offset) {
var navigationLink = highlightNavigation.sectionIdTonavigationLink[currentSection.id];
var navigationListItem = utilities.closest(navigationLink, "li");
if (navigationListItem && !navigationListItem.classList.contains("active")) {
for (var i = 0; i < highlightNavigation.navigationListItems.length; i++) {
var el = highlightNavigation.navigationListItems[i];
if (el.classList.contains("active")) {
el.classList.remove("active");
}
}
navigationListItem.classList.add("active");
// Scroll to active item. Not a requested feature but we could revive it. Needs work.
// var menuTop = $("#pytorch-right-menu").position().top;
// var itemTop = navigationListItem.getBoundingClientRect().top;
// var TOP_PADDING = 20
// var newActiveTop = $("#pytorch-side-scroll-right").scrollTop() + itemTop - menuTop - TOP_PADDING;
// $("#pytorch-side-scroll-right").animate({
// scrollTop: newActiveTop
// }, 100);
}
break;
}
}
}
};