Back to all resources
tabs
hover events
dynamic content display

Tabs switch on hover

By Andrea Morgan
This JavaScript code snippet is designed to manage a tabbed interface where multiple tabs are available, but only the content of one tab is displayed at a time. The tabs are manipulated through hover interactions rather than clicks.

  • Initialization: The code first selects all elements with the class all-features_tab-link (representing the tab links) and all-features_tab-pane (representing the tab content areas) and stores them in tabLinks and tabContents respectively.
  • Default State Setup: It hides all tab content areas except the first one using jQuery's not(':first').hide() method. This ensures that when the page loads, only the content of the first tab is visible.
  • Hover Event Handling: A hover event listener is attached to the tab links. When a tab link is hovered over:
    • It removes the class w--current from all tab links, which might be used to highlight the active tab.
    • It adds the class w--current to the tab that is currently being hovered over, highlighting it as active.
    • It hides all tab content areas to ensure that content from a previously selected tab is not visible.
    • It calculates the index of the currently hovered tab link and shows the corresponding tab content area by matching the index of the tab link to the index of the tab content.

This setup allows for a dynamic user interaction where hovering over different tabs displays their respective content areas without the need to click.