Support » Theme: Twenty Seventeen » Sticky mobile menu

  • Resolved dEM0nsTAr

    (@dem0nstar)


    How can I make the menu to be sticky at the top for mobile phones while scrolling down?
    Thanks in advance!

Viewing 11 replies - 16 through 26 (of 26 total)
  • @rfortin,
    This will be the code for my custom js file ( sticky_mobile_menu.js ) after I update, withstanding that the search code is placed correctly.

    // JavaScript Document
    // sticky_mobile_menu.js
    
    // /**
    // * Sticky mobile menu, theme Twenty Seventeen
    // * @uri https://wordpress.org/support/topic/sticky-mobile-menu/
    // */
    
    (function($) {
    
    $(‘#top-menu li a’).click( function(){
    $(‘#site-navigation’).attr(‘class’,’main-navigation’);
    });
    // All future jquery code can go in here
    
    })( jQuery );
    
    // I have a search currently under the header at above site. Is there a way to make sticky the search under the sticky menu, both at the top?
    // Try adding the following code in your custom js file:
    $('#search-container').detach().appendTo('.navigation-top .wrap');
    
    

    Does the placement of
    $('#search-container').detach().appendTo('.navigation-top .wrap');
    Need to be moved anywhere in the custom js file, or is it fine as shown?

    $('#search-container').detach().appendTo('.navigation-top .wrap');

    should go inside the main function, where it is stated “all future jquery code can go in here”.

    (function($) {
    
    $(‘#top-menu li a’).click( function(){
      $(‘#site-navigation’).attr(‘class’,’main-navigation’);
    });
    $('#search-container').detach().appendTo('.navigation-top .wrap');
    
    })( jQuery );

    Anything you want to do with jquery when the page loads, you put it there. Good luck!

    Thanks!
    I updated the placement.

    Without luck, no placement has changed of the search section.
    On my desktop, I cleared local caches, reloaded the view, and purged all cache from the plugin W3TC, your Web Performance Optimization, to no avail. I may have to wait for my server to give it some time before the changes show? Not sure.
    I’ll keep checking for the next few days to see if the updated changes have taken effect.
    Thanks for all the help so far @rfortin.

    I checked your site and it doesn’t look like your js file is being loaded.

    Add an

    alert('You will see this alert if the file is loaded properly');

    inside the main function of your sticky_mobile_menu.js. If you don’t see the alert on page load, then there’s likely a problem with your functions.php where you enqueue sticky_mobile_menu.js

    🙁
    I hadn’t seen the alert load after adding the code into the function in sticky_mobile_menu.js. This is what the end of my functions.php looks like where the enqueue scripts are called.

    /**
    * Combining enqueue functions
    @link https://developer.wordpress.org/themes/basics/including-css-javascript/
    */
    function add_theme_scripts() {
      wp_enqueue_style( 'style', get_stylesheet_uri() );
    	
      wp_enqueue_script( 'script', get_stylesheet_uri() . '/v1984/js/v1984.js', array ( 'jquery' ), true);
    	
        if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
          wp_enqueue_script( 'comment-reply' );
        }
    }
    add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );
    
    /**
     * Sticky mobile menu, theme Twenty Seventeen
     * @uri https://wordpress.org/support/topic/sticky-mobile-menu/
     */
    
    /*************************** Custom js ***************************/
    function my_custom_js() {
        wp_enqueue_script( 'sticky_mobile_menu', get_stylesheet_directory_uri() . '/v1984/js/sticky_mobile_menu.js', array( 'jquery' ), '1.0.0', true );
    }
    add_action( 'wp_enqueue_scripts', 'my_custom_js' );
    
    ?>

    I reviewed my custom js file again, and it appears that some formats of the open and close single quotes were different in the first section that in added section for the search. Perhaps this quirk effected things. The update now is as follows.

    // JavaScript Document
    // sticky_mobile_menu.js
    
    // /**
    // * Sticky mobile menu, theme Twenty Seventeen
    // * @uri https://wordpress.org/support/topic/sticky-mobile-menu/
    // */
    
    (function($) {
    
    $(‘#top-menu li a’).click( function(){
    $(‘#site-navigation’).attr(‘class’,’main-navigation’);
    });
    // I have a search currently under the header at above site. Is there a way to make sticky the search under the sticky menu, both at the top?
    // Try adding the following code in your custom js file:
    $(‘#search-container’).detach().appendTo(‘.navigation-top .wrap’);
    alert(‘You will see this alert if the file is loaded properly’);
    // All future jquery code can go in here
    
    })( jQuery );
    

    Hi @rfortin I have added your css code to make the menubar sticky on mobile view and it worked.
    Now I want to implement the jquery solution for closing the menu automatically after clicking the link but I am not familiar with how and where to add .js file for this.

    Could you please guide me through this?

    Sure. The following assumes you have a child theme with a correctly set up functions.php. If not, follow the guide here https://www.smashingmagazine.com/2016/01/create-customize-wordpress-child-theme/

    1. Go to your file manager and find your theme’s child folder
    2. Create a new folder called JS and inside create a file with js extension. For example, mine is custom_js.js (here’s my path for example: …/public_html/wp-content/themes/twentyseventeen-child2/js/custom_js.js)
    3. Inside your js file, write:

    (function($) {
    
    // This is where you type any jquery code you like. 
    
    // The following code closes the mobile menu on link click 
      var $menu_link = $('#top-menu li a'); 
      $menu_link.click( function(){
        $('#site-navigation').removeClass('toggled-on');
      });
    
    })( jQuery );

    4. In your functions.php file (inside the <?php and ?> , add the following code– replacing custom_js to whatever filename you chose in step 2

    function my_custom_js() {
        wp_enqueue_script( 'custom_js', get_stylesheet_directory_uri() . '/js/custom_js.js', array( 'jquery' ), '1.0.0', true );
    }
    add_action( 'wp_enqueue_scripts', 'my_custom_js' );

    thank you @rfortin the CSS works nice.

    Not sure why but I have no issues with the menu.. it closes after selecting an item.

    • This reply was modified 4 years, 2 months ago by ramonmobiel.
    paulscapes

    (@paulscapes)

    Thank you @rfortin I could not expect a better solution. Great !

Viewing 11 replies - 16 through 26 (of 26 total)
  • The topic ‘Sticky mobile menu’ is closed to new replies.