MediaWiki:Gadget-SidebarTOC.js

Revision as of 07:20, 22 February 2024 by Makko Oko (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
mw.loader.using( ['jquery', 'mediawiki.util'], function () {
    $( function () {
        var $toc = $( '#toc' );

        if ( !$toc.length ) {
            return;
        }

        var $mwContent = $( '#mw-content-text' );
        var $floatTOC = $toc
            .clone()
            .removeAttr( 'id' )
            .addClass( 'floatTOC' )
            .appendTo( $mwContent )
            .css( {
                height: $mwContent.height(), // Set ToC height to match article height
                overflowY: 'auto' // Enable vertical scrolling if ToC exceeds article height
            } );

        // Update scrollHandler function to use $mwContent instead of window
        var scrollHandler = function () {
            var scrollTop = $mwContent.scrollTop(); // Use scrollTop of $mwContent

            if ( scrollTop > 0 ) { // Adjust condition based on scroll position
                $floatTOC.css( 'visibility', 'visible' );
            } else {
                $floatTOC.css( 'visibility', 'hidden' );
            }
        };

        // Update event binding to use $mwContent instead of window
        $mwContent.on( 'scroll', scrollHandler );
    } );
} );