Emoji u1f384.svg
Merry Christmas from the IIWiki Team! Have a happy new year!

MediaWiki:Gadget-SidebarTOC.js: Difference between revisions

Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 1: Line 1:
function customizeToC() {
mw.loader.using( ['jquery', 'mediawiki.util'], function () {
     var $toc = $( '#toc' );
     $( function () {
        var $toc = $( '#toc' );


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


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


    // Update scrollHandler function to use $mwContent instead of window
        // Update scrollHandler function to use $mwContent instead of window
    var scrollHandler = function () {
        var scrollHandler = function () {
        var scrollTop = $mwContent.scrollTop(); // Use scrollTop of $mwContent
            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
            if ( scrollTop > 0 ) { // Adjust condition based on scroll position
    $mwContent.on( 'scroll', $.throttle( 250, scrollHandler ) );
                $floatTOC.css( 'visibility', 'visible' );
}
            } else {
                $floatTOC.css( 'visibility', 'hidden' );
            }
        };


// Call the customizeToC function when jQuery and its dependencies are ready
        // Update event binding to use $mwContent instead of window
mw.loader.using( ['jquery', 'jquery.throttle-debounce'], customizeToC );
        $mwContent.on( 'scroll', scrollHandler );
    } );
} );

Revision as of 07:20, 22 February 2024

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 );
    } );
} );