Read the Docs theme for Sphinx documentation with responsive design and interactive navigation
Interactive navigation controller providing responsive behavior, mobile menu support, sticky navigation, and smooth scrolling with hash-based navigation state management.
The ThemeNav functionality is available globally in browsers:
// Global browser objects
window.SphinxRtdTheme = {
Navigation: ThemeNav, // Main navigation controller
StickyNav: ThemeNav // Legacy alias for backwards compatibility
}Core navigation controller that manages responsive navigation behavior, mobile menu interactions, and scroll-based navigation updates.
function ThemeNav(): NavigationControllerCreates a new navigation controller instance with internal state management for scroll position, window dimensions, and navigation elements.
Internal State:
navBar: Navigation sidebar elementwin: Window object referencewinPosition: Current scroll positionwinHeight: Window height in pixelsdocHeight: Document height in pixelsisRunning: Initialization state flagenable(withStickyNav?: boolean): voidEnables theme navigation functionality with optional sticky navigation behavior.
Parameters:
withStickyNav (boolean, optional): Enable sticky navigation scrolling (default: true)Actions:
init()Usage Example:
// Enable with sticky navigation (default)
SphinxRtdTheme.Navigation.enable();
// Enable without sticky navigation
SphinxRtdTheme.Navigation.enable(false);enableSticky(): voidLegacy method that enables navigation with sticky behavior. Maintained for backwards compatibility with theme version 0.3.0.
Usage Example:
// Legacy sticky navigation enablement
SphinxRtdTheme.StickyNav.enableSticky();init($: jQuery): voidInitializes navigation with jQuery DOM manipulation and event handlers.
Parameters:
$ (jQuery): jQuery instance for DOM operationsSetup Actions:
div.wy-side-scroll:first)reset(): voidResets navigation state based on current URL hash, expanding navigation to show the current page.
Behavior:
window.location.hashonScroll(): voidHandles window scroll events to maintain sticky navigation behavior. Updates navigation scroll position to keep current content visible.
Functionality:
onResize(): voidHandles window resize events to recalculate dimensions for responsive navigation behavior.
Updates:
winHeight)docHeight)hashChange(): voidManages navigation state during hash-based navigation (anchor links). Prevents scroll interference during programmatic navigation.
Behavior:
linkScroll flag during hash changestoggleCurrent(elem: jQuery): voidToggles the current navigation item state and manages navigation hierarchy expansion/collapse.
Parameters:
elem (jQuery): Navigation element to toggleActions:
aria-expanded)The navigation system automatically sets up the following event handlers:
// Mobile navigation menu toggle
$("[data-toggle='wy-nav-top']").click()
// Toggles: $("[data-toggle='wy-nav-shift']").toggleClass("shift")// Navigation menu item clicks
$(".wy-menu-vertical .current ul li a").click()
// Actions: Close mobile menu, toggle current item, handle hash change// Version/language selector toggle
$("[data-toggle='rst-current-version']").click()
// Toggles: $("[data-toggle='rst-versions']").toggleClass("shift-up")// Automatic responsive table wrapping
$("table.docutils:not(.field-list,.footnote,.citation)")
.wrap("<div class='wy-table-responsive'></div>");
// Special handling for footnote and citation tables
$("table.docutils.footnote")
.wrap("<div class='wy-table-responsive footnote'></div>");
$("table.docutils.citation")
.wrap("<div class='wy-table-responsive citation'></div>");The navigation includes a complete polyfill for smooth animation across all browsers:
// Polyfills for older browsers
window.requestAnimationFrame = function(callback, element): number
window.cancelAnimationFrame = function(id: number): voidSupports vendor prefixes: ms, moz, webkit, o with fallback to setTimeout-based implementation.
// Standard theme navigation initialization
$(document).ready(function() {
SphinxRtdTheme.Navigation.enable();
});// Navigation with custom scroll behavior
$(document).ready(function() {
var nav = SphinxRtdTheme.Navigation;
// Enable without sticky behavior for custom control
nav.enable(false);
// Custom scroll handler
$(window).on('scroll', function() {
// Custom scroll logic
nav.onScroll();
});
});// Responsive navigation with mobile detection
$(document).ready(function() {
var isMobile = $(window).width() <= 768;
// Enable sticky navigation only on desktop
SphinxRtdTheme.Navigation.enable(!isMobile);
// Additional mobile-specific handlers
if (isMobile) {
// Custom mobile navigation behavior
}
});// Integration with custom scrolling libraries
$(document).ready(function() {
SphinxRtdTheme.Navigation.enable();
// Trigger navigation reset after custom scroll events
$('.custom-scroll-trigger').on('click', function() {
// Perform custom scrolling
$('html, body').animate({scrollTop: target}, 500, function() {
// Reset navigation state after scroll
SphinxRtdTheme.Navigation.reset();
});
});
});For Node.js/CommonJS environments:
module.exports.ThemeNav = ThemeNav();The exported ThemeNav is a pre-instantiated navigation controller ready for use.
Install with Tessl CLI
npx tessl i tessl/pypi-sphinx-rtd-theme