Skip to content
  • Recent
  • Tags
  • Popular
  • Users
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

Lay Theme Forum

  1. Home
  2. General Discussion
  3. Collapse mobile navigation after click

Collapse mobile navigation after click

Scheduled Pinned Locked Moved General Discussion
6 Posts 2 Posters 142 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    miriamb
    wrote on last edited by
    #1

    Hello,

    I am building a very simple one-pager with three anchors that you can jump to in the menu. That also works quite fine with the mobile burger navigation. But I want the navigation to close after a menu item has been clicked.
    To close the navigation after clicking on one menu item I used some code line I found here:
    http://laythemeforum.com:4567/topic/4005/collapse-mobile-nav-on-click-menu-item but

    But sadly the suggested solution doesn't work as expected. It actually messes up the scrollTo action, so now it scrolls up to the top and than to the anchor. And I don't understand why. I also tried the suggested toggleClass, but it worked even less.

    This is the script I have in Custom HTML at bottom:
    <script>
    jQuery(document).ready(function(){
    jQuery('body').on('click', 'li.menu-item > a', function(event) {
    event.preventDefault();
    var id = jQuery(this).attr('href');
    jQuery('html, body').animate( { scrollTop: jQuery(id).offset().top }, 750 );
    });

    jQuery(document).on("click", ".mobile-nav ul li a", function(event) {
    jQuery(".mobile-nav").removeClass('active');
    jQuery(".mobile-nav").css('transform','translateY(-180px)');
    });
    });
    </script>

    This is the link to the page I am talking about: http://wendelstein-steuerberatung.de

    I am very much looking forward to your answer.
    Best regards, Miriam

    1 Reply Last reply
    0
    • RichardR Offline
      RichardR Offline
      Richard
      Global Moderator
      wrote on last edited by
      #2

      Dear Miriam

      @miriamb

      Did you find a solution? :)

      <script>
      jQuery(document).ready(function(){
      jQuery('body').on('click', 'li.menu-item > a', function(event) {
      event.preventDefault();
      var id = jQuery(this).attr('href');
      jQuery('html, body').animate( { scrollTop: jQuery(id).offset().top }, 750 );
      });
      
      jQuery(document).on("click", ".mobile-nav ul li a", function(event) {
      jQuery(".mobile-nav").removeClass('active');
      jQuery(".mobile-nav").css('transform','translateY(-180px)');
      });
      });
      </script>
      

      Best Wishes
      Richard

      1 Reply Last reply
      0
      • M Offline
        M Offline
        miriamb
        wrote on last edited by
        #3

        Hi Richard,

        yes, I deleted the upper part of the script. It probably duplicated the default settings in the theme and was the reason for the interference. It works fine now.

        <script>
        jQuery(document).ready(function(){

        jQuery(document).on("click", ".mobile-nav ul li a", function(event) {
        jQuery(".mobile-nav").removeClass('active');
        jQuery(".mobile-nav").css('transform','translateY(-180px)');
        });

        });
        </script>

        But as usual a new problem appeared. šŸ˜… If an item is clicked in the menu and the menu is disappeared, the "close-x" remains (see screenshot). Instead, the burger menu should appear again. Do you have an idea how to solve this?

        Thanks for your help again,
        Miriam

        IMG_E42DFDA45A6F-1.jpeg

        1 Reply Last reply
        0
        • RichardR Offline
          RichardR Offline
          Richard
          Global Moderator
          wrote on last edited by
          #4

          Dear Miriam @miriamb

          Your code is good! :)

          The Mobile Icon "burger" also gets a class .active added and removed. This class .active gives new CSS telling it to change shape, you can see here the class be added and removed on click:
          Screen Shot 2021-03-31 at 6.42.01 PM.png
          Screen Shot 2021-03-31 at 6.42.11 PM.png

          Here in the below code i have added that the 'mobile-menu-icon' must also remove its class .active when you click & that way it will return to its original state:

          jQuery(document).ready(function(){
          
          jQuery(document).on("click", ".mobile-nav ul li a", function(event) {
          jQuery(".mobile-nav").removeClass('active');
          jQuery(".mobile-menu-icon").removeClass('active');
          jQuery(".mobile-nav").css('transform','translateY(-180px)');
          });
          
          });
          

          Now when you click a menu-item the .active should be removed from the mobile-icon and all should work as desired!

          Hope this helps Miriam and have a wonderful day - Website looks great :)

          Sincerely
          Richard

          M 1 Reply Last reply
          0
          • RichardR Richard

            Dear Miriam @miriamb

            Your code is good! :)

            The Mobile Icon "burger" also gets a class .active added and removed. This class .active gives new CSS telling it to change shape, you can see here the class be added and removed on click:
            Screen Shot 2021-03-31 at 6.42.01 PM.png
            Screen Shot 2021-03-31 at 6.42.11 PM.png

            Here in the below code i have added that the 'mobile-menu-icon' must also remove its class .active when you click & that way it will return to its original state:

            jQuery(document).ready(function(){
            
            jQuery(document).on("click", ".mobile-nav ul li a", function(event) {
            jQuery(".mobile-nav").removeClass('active');
            jQuery(".mobile-menu-icon").removeClass('active');
            jQuery(".mobile-nav").css('transform','translateY(-180px)');
            });
            
            });
            

            Now when you click a menu-item the .active should be removed from the mobile-icon and all should work as desired!

            Hope this helps Miriam and have a wonderful day - Website looks great :)

            Sincerely
            Richard

            M Offline
            M Offline
            miriamb
            wrote on last edited by
            #5

            Hi @Richard,
            thank you so much for your help and kind words. All works perfectly fine now. It's lots of fun working with lay theme! šŸ˜
            Best, Miriam

            1 Reply Last reply
            0
            • RichardR Offline
              RichardR Offline
              Richard
              Global Moderator
              wrote on last edited by
              #6

              Dear Miriam @miriamb

              Great to hear that its working!
              Have a lovely day & best wishes

              Richard

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              I also code custom websites or custom Lay features.
              šŸ’æ Email me here: šŸ’æ
              info@laytheme.com

              Before you post:
              1. When using a WordPress Cache plugin, disable it or clear your cache.
              2. Update Lay Theme and all Lay Theme Addons
              3. Disable all Plugins
              4. Go to Lay Options → Custom CSS & HTML, click "Turn Off All Custom Code", click "Save Changes"

              This often solves issues you might run into

              When you post:
              1. Post a link to where the problem is
              2. Does the problem happen on Chrome, Firefox, Safari or iPhone or Android?
              3. If the problem is difficult to explain, post screenshots / link to a video to explain it
              Online Users
              Forgot your key, lost your files, need a previous Lay Theme or Addon version? Go to www.laykeymanager.com
              laytheme.com
              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Recent
              • Tags
              • Popular
              • Users
              • Search