Make WordPress Core

Changeset 49855


Ignore:
Timestamp:
12/21/2020 03:14:15 PM (4 years ago)
Author:
desrosj
Message:

Twenty Twenty-One: Allow local anchor links to be used in primary navigation.

This adds some JavaScript to detect when an anchor link is clicked within the primary navigation on mobile devices and closes the menu before scrolling to the location on the page.

Props poena, macmanx, t-p.
Merges [49854] to the 5.6 branch.
Fixes #52006.

Location:
branches/5.6
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.6

  • branches/5.6/src/wp-content/themes/twentytwentyone/assets/js/primary-navigation.js

    r49829 r49855  
    151151        } );
    152152
     153        /**
     154         * Close menu and scroll to anchor when an anchor link is clicked.
     155         * Adapted from TwentyTwenty.
     156         */
     157        document.addEventListener( 'click', function( event ) {
     158            // If target onclick is <a> with # within the href attribute
     159            if ( event.target.hash && event.target.hash.includes( '#' ) ) {
     160                wrapper.classList.remove( id + '-navigation-open', 'lock-scrolling' );
     161                twentytwentyoneToggleAriaExpanded( mobileButton );
     162                // Wait 550 and scroll to the anchor.
     163                setTimeout(function () {
     164                    var anchor = document.getElementById(event.target.hash.slice(1));
     165                    anchor.scrollIntoView();
     166                }, 550);
     167            }
     168        } );
     169
    153170        document.getElementById( 'site-navigation' ).querySelectorAll( '.menu-wrapper > .menu-item-has-children' ).forEach( function( li ) {
    154171            li.addEventListener( 'mouseenter', function() {
Note: See TracChangeset for help on using the changeset viewer.