Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keras-team
GitHub Repository: keras-team/keras-io
Path: blob/master/theme/js/index.js
3283 views
1
// Mobile nav controls
2
const navButton = document.querySelector('.nav__menu--button');
3
const closeButton = document.querySelector('.nav__menu--close');
4
const mobileNavMenu = document.querySelector('.k-nav');
5
const pageContainer = document.querySelector('.page__container');
6
7
navButton.addEventListener('click', () => {
8
mobileNavMenu.style.display = 'block';
9
closeButton.style.display = 'block';
10
navButton.style.display = 'none';
11
pageContainer.style.position = 'fixed';
12
});
13
14
closeButton.addEventListener('click', () => {
15
mobileNavMenu.style.display = 'none';
16
closeButton.style.display = 'none';
17
navButton.style.display = 'block';
18
pageContainer.style.position = 'static';
19
});
20
21
// Copy code
22
const copyButtons = document.querySelectorAll('.code__copy--button');
23
copyButtons.forEach((button) => {
24
button.addEventListener('click', () => {
25
const parent = button.parentNode;
26
const text = parent.querySelector('.language-python').innerText;
27
const inputElement = document.createElement('textarea');
28
console.log('text', text);
29
inputElement.value = text;
30
inputElement.setAttribute('class', 'visually-hidden');
31
const body = document.body;
32
body.appendChild(inputElement);
33
inputElement.select();
34
document.execCommand('copy');
35
inputElement.remove();
36
37
button.querySelector('.code__copy--tooltip').style.display = 'block';
38
setTimeout(() => {
39
button.querySelector('.code__copy--tooltip').style.display = 'none';
40
}, 2000);
41
});
42
});
43
44
// Search controls
45
const searchForms = document.querySelectorAll('.nav__search');
46
const mobileNavSearchIcon = document.querySelector('.nav__search--mobile');
47
const mobileNavSearchForm = document.querySelector('.nav__search-form--mobile');
48
const mobileNavControls = document.querySelector('.nav__controls--mobile');
49
const desktopSearch = document.querySelector('.nav__menu .nav__search');
50
51
mobileNavSearchIcon.addEventListener('click', () => {
52
mobileNavControls.style.display = 'none';
53
mobileNavSearchForm.style.display = 'block';
54
});
55
56
searchForms.forEach((search) => {
57
search.addEventListener('submit', (event) => {
58
event.preventDefault();
59
const text = search.querySelector('.nav__search--input').value;
60
window.location = `/search.html?query=${text}`;
61
});
62
});
63
64
pageContainer.addEventListener('click', () => {
65
mobileNavControls.style.display = 'flex';
66
mobileNavSearchForm.style.display = 'none';
67
});
68
69
mobileNavMenu.addEventListener('click', () => {
70
mobileNavControls.style.display = 'flex';
71
mobileNavSearchForm.style.display = 'none';
72
});
73
74
if (window.location.pathname.indexOf('search.html') > -1) {
75
desktopSearch.style.display = 'none';
76
mobileNavSearchIcon.style.visibility = 'hidden';
77
}
78
79
// position:sticky functionality (set margin-top so that the content is correctly centered vertically)
80
const exploreModule = document.querySelector('.explore');
81
const exploreContent = document.querySelector('.explore__content');
82
83
const observer = new IntersectionObserver(
84
(entries) => {
85
entries.forEach((entry) => {
86
if (entry.isIntersecting) {
87
window.addEventListener('resize', verticallyCenterExploreContent);
88
return;
89
}
90
91
window.removeEventListener('resize', verticallyCenterExploreContent);
92
});
93
},
94
{ threshold: 0 }
95
);
96
97
if (exploreModule && window.innerWidth > 1199) {
98
observer.observe(exploreModule);
99
/* let's call it once initially to align it in case a screen never gets resized */
100
verticallyCenterExploreContent();
101
}
102
103
function verticallyCenterExploreContent() {
104
exploreContent.style.marginTop = `${Math.round(exploreContent.getBoundingClientRect().height / 2)}px`;
105
}
106
107