Skip to content

Commit 0d781e4

Browse files
authored
Make sidebar scrollable and sticky (on modern browsers) (#91)
Just make the sidebar stick to the top with CSS and show a scrollbar on overflow. There's no fallback for older browsers -- since up to now, this didn't work for anyone (because "intelligent" scrolling has been broken for years), I assume improving the UX at least for modern browsers is still a net improvement.
1 parent efb6c69 commit 0d781e4

File tree

2 files changed

+20
-56
lines changed

2 files changed

+20
-56
lines changed

python_docs_theme/static/pydoctheme.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ form.inline-search input[type="submit"] {
5151
width: 40px;
5252
}
5353

54+
div.document {
55+
display: flex;
56+
}
57+
5458
div.sphinxsidebar {
59+
float: none;
60+
position: sticky;
61+
top: 0;
62+
max-height: 100vh;
5563
background-color: #eeeeee;
5664
border-radius: 5px;
5765
line-height: 130%;
@@ -62,6 +70,13 @@ div.sphinxsidebar h3, div.sphinxsidebar h4 {
6270
margin-top: 1.5em;
6371
}
6472

73+
div.sphinxsidebarwrapper {
74+
box-sizing: border-box;
75+
height: 100%;
76+
overflow-x: hidden;
77+
overflow-y: auto;
78+
}
79+
6580
div.sphinxsidebarwrapper > h3:first-child {
6681
margin-top: 0.2em;
6782
}

python_docs_theme/static/sidebar.js

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* sidebar.js
33
* ~~~~~~~~~~
44
*
5-
* This script makes the Sphinx sidebar collapsible and implements intelligent
6-
* scrolling. This is a slightly modified version of Sphinx's own sidebar.js.
5+
* This script makes the Sphinx sidebar collapsible. This is a slightly
6+
* modified version of Sphinx's own sidebar.js.
77
*
88
* .sphinxsidebar contains .sphinxsidebarwrapper. This script adds in
99
* .sphixsidebar, after .sphinxsidebarwrapper, the #sidebarbutton used to
@@ -25,10 +25,7 @@ $(function() {
2525
// global elements used by the functions.
2626
// the 'sidebarbutton' element is defined as global after its
2727
// creation, in the add_sidebar_button function
28-
var jwindow = $(window);
29-
var jdocument = $(document);
3028
var bodywrapper = $('.bodywrapper');
31-
var documentwrapper = $('.documentwrapper');
3229
var sidebar = $('.sphinxsidebar');
3330
var sidebarwrapper = $('.sphinxsidebarwrapper');
3431

@@ -46,13 +43,6 @@ $(function() {
4643
var dark_color = '#AAAAAA';
4744
var light_color = '#CCCCCC';
4845

49-
function get_viewport_height() {
50-
if (window.innerHeight)
51-
return window.innerHeight;
52-
else
53-
return jwindow.height();
54-
}
55-
5646
function sidebar_is_collapsed() {
5747
return sidebarwrapper.is(':not(:visible)');
5848
}
@@ -62,8 +52,6 @@ $(function() {
6252
expand_sidebar();
6353
else
6454
collapse_sidebar();
65-
// adjust the scrolling of the sidebar
66-
scroll_sidebar();
6755
}
6856

6957
function collapse_sidebar() {
@@ -72,7 +60,6 @@ $(function() {
7260
bodywrapper.css('margin-left', bw_margin_collapsed);
7361
sidebarbutton.css({
7462
'margin-left': '0',
75-
'height': documentwrapper.height(),
7663
'border-radius': '5px'
7764
});
7865
sidebarbutton.find('span').text('»');
@@ -86,35 +73,28 @@ $(function() {
8673
sidebarwrapper.show();
8774
sidebarbutton.css({
8875
'margin-left': ssb_width_expanded-12,
89-
'height': Math.max(sidebarwrapper.height(), documentwrapper.height()),
9076
'border-radius': '0 5px 5px 0'
9177
});
9278
sidebarbutton.find('span').text('«');
9379
sidebarbutton.attr('title', _('Collapse sidebar'));
94-
//sidebarwrapper.css({'padding-top':
95-
// Math.max(window.pageYOffset - sidebarwrapper.offset().top, 10)});
9680
document.cookie = 'sidebar=expanded';
9781
}
9882

9983
function add_sidebar_button() {
10084
sidebarwrapper.css({
10185
'float': 'left',
10286
'margin-right': '0',
103-
'width': ssb_width_expanded - 28
87+
'width': ssb_width_expanded - 13
10488
});
10589
// create the button
10690
sidebar.append(
10791
'<div id="sidebarbutton"><span>&laquo;</span></div>'
10892
);
10993
var sidebarbutton = $('#sidebarbutton');
110-
// find the height of the viewport to center the '<<' in the page
111-
var viewport_height = get_viewport_height();
112-
var sidebar_offset = sidebar.offset().top;
113-
var sidebar_height = Math.max(documentwrapper.height(), sidebar.height());
11494
sidebarbutton.find('span').css({
11595
'display': 'block',
11696
'position': 'fixed',
117-
'top': Math.min(viewport_height/2, sidebar_height/2 + sidebar_offset) - 10
97+
'top': '50%'
11898
});
11999

120100
sidebarbutton.click(toggle_sidebar);
@@ -125,8 +105,7 @@ $(function() {
125105
'background-color': '#CCCCCC',
126106
'font-size': '1.2em',
127107
'cursor': 'pointer',
128-
'height': sidebar_height,
129-
'padding-top': '1px',
108+
'height': '100%',
130109
'padding-left': '1px',
131110
'margin-left': ssb_width_expanded - 12
132111
});
@@ -161,34 +140,4 @@ $(function() {
161140
add_sidebar_button();
162141
var sidebarbutton = $('#sidebarbutton');
163142
set_position_from_cookie();
164-
165-
166-
/* intelligent scrolling */
167-
function scroll_sidebar() {
168-
var sidebar_height = sidebarwrapper.height();
169-
var viewport_height = get_viewport_height();
170-
var offset = sidebar.position()['top'];
171-
var wintop = jwindow.scrollTop();
172-
var winbot = wintop + viewport_height;
173-
var curtop = sidebarwrapper.position()['top'];
174-
var curbot = curtop + sidebar_height;
175-
// does sidebar fit in window?
176-
if (sidebar_height < viewport_height) {
177-
// yes: easy case -- always keep at the top
178-
sidebarwrapper.css('top', $u.min([$u.max([0, wintop - offset - 10]),
179-
jdocument.height() - sidebar_height - 200]));
180-
}
181-
else {
182-
// no: only scroll if top/bottom edge of sidebar is at
183-
// top/bottom edge of window
184-
if (curtop > wintop && curbot > winbot) {
185-
sidebarwrapper.css('top', $u.max([wintop - offset - 10, 0]));
186-
}
187-
else if (curtop < wintop && curbot < winbot) {
188-
sidebarwrapper.css('top', $u.min([winbot - sidebar_height - offset - 20,
189-
jdocument.height() - sidebar_height - 200]));
190-
}
191-
}
192-
}
193-
jwindow.scroll(scroll_sidebar);
194143
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy