Skip to content

File tree

2 files changed

+66
-54
lines changed

2 files changed

+66
-54
lines changed

ptoc.css

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
transition: 1s all ease;
33
display: block;
44
position: fixed;
5-
top: 0px;
6-
left: 0px;
7-
border: none !important;
8-
background: #f6f6f6 !important;
9-
font-size: 0.8em !important;
10-
min-width: 162px;
11-
width: 162px;
5+
top: 0;
6+
left: 0;
7+
border: none;
8+
background: #f6f6f6;
9+
font-size: 0.8em;
10+
width: 10em;
11+
height: 100%;
1212
white-space: nowrap;
1313
overflow: hidden;
1414
}
1515

1616
.floatTOC ul ul {
17-
margin: 0 0 0 1em
17+
margin: 0 0 0 1em;
1818
}
1919

2020
.floatTOC:hover {
2121
width: auto;
2222
overflow: auto;
23-
box-shadow: 0 0 30px rgba( 0,0,0, 0.5) ;
23+
box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
2424
}
2525

2626
.floatTOC > * {
@@ -29,6 +29,5 @@
2929
}
3030

3131
.floatTOC:hover > * {
32-
transition: 1s all ease;
3332
opacity: 1;
3433
}

ptoc.js

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,62 @@
1-
( function ( $ ) {
2-
3-
$( function () {
4-
var $toc = $( '#toc' ),
5-
$window = $( window ),
6-
$mwPanel = $( '#mw-panel' ),
7-
tocLimit = $toc.offset().top + $toc.height(),
8-
$floatTOC = $toc
9-
.clone()
10-
.attr( 'id', '')
11-
.addClass( 'floatTOC' )
12-
.appendTo( 'body' )
13-
.css( 'height', $window.height() )
14-
.hide(),
15-
headingsOffset = [];
16-
17-
// Get all heading positions
18-
$('.mw-headline').each( function() {
19-
headingsOffset.push( [ $(this).attr( 'id' ), $( this ).offset().top ] );
20-
} );
1+
mw.loader.using( 'jquery.throttle-debounce', function () {
2+
$( function () {
3+
var $window, $mwPanel, $floatTOC, scrollHandler,
4+
tocLimit, headingOffsets,
5+
$toc = $( '#toc' );
6+
7+
if ( !$toc.length ) {
8+
return;
9+
}
10+
11+
$window = $( window );
12+
$mwPanel = $( '#mw-panel' );
13+
$floatTOC = $toc
14+
.clone()
15+
.removeAttr( 'id' )
16+
.addClass( 'floatTOC' )
17+
.appendTo( 'body' )
18+
.hide();
19+
20+
tocLimit = $toc.offset().top + $toc.height();
21+
headingOffsets = [];
2122

22-
// For the window scroll event
23-
var scroller = function () {
24-
var scrollTop = $window.scrollTop();
25-
if ( scrollTop > tocLimit ) {
26-
$floatTOC.show();
27-
$mwPanel.hide();
28-
29-
// Highlight current
30-
$floatTOC.find( 'a' ).css( 'font-weight', 'normal' )
31-
var highlight = "";
32-
$.each( headingsOffset, function ( i, v ) {
33-
if ( scrollTop < v[ 1 ]) {
34-
highlight = headingsOffset[ i -1 ][ 0 ];
35-
return false;
36-
}
23+
// Get all heading positions
24+
$('.mw-headline').each( function () {
25+
headingOffsets.push( [ $( this ).attr( 'id' ), $( this ).offset().top ] );
3726
} );
38-
$floatTOC.find( 'a[href="#' + highlight + '"]' ).css( 'font-weight', 'bold' );
3927

40-
} else {
41-
$floatTOC.hide();
42-
$mwPanel.show();
43-
}
44-
}
28+
// For the window scroll event
29+
scrollHandler = function () {
30+
var $current,
31+
scrollTop = $window.scrollTop();
4532

46-
$window.on( 'scroll', $.throttle( 250, scroller ) );
47-
} );
33+
if ( scrollTop > tocLimit ) {
34+
$floatTOC.show();
35+
$mwPanel.hide();
4836

49-
} ) ( jQuery );
37+
// Highlight current
38+
var highlight = false;
39+
// Current section is above the first heading below the top of the screen
40+
$.each( headingOffsets, function ( i, v ) {
41+
// Skip first as there's no previous heading before the first
42+
if ( i !== 0 && scrollTop < v[ 1 ] ) {
43+
highlight = headingOffsets[ i - 1 ][ 0 ];
44+
return false;
45+
}
46+
} );
47+
48+
if ( highlight ) {
49+
$current = $floatTOC.find( 'a[href="#' + highlight + '"]' );
50+
$floatTOC.find( 'a' ).not( $current ).css( 'font-weight', '' );
51+
$current.css( 'font-weight', 'bold' );
52+
}
53+
54+
} else {
55+
$floatTOC.hide();
56+
$mwPanel.show();
57+
}
58+
}
59+
60+
$window.on( 'scroll', $.throttle( 250, scrollHandler ) );
61+
} );
62+
} );

0 commit comments

Comments
 (0)