Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:00:29+00:00 2026-06-14T00:00:29+00:00

I have this code which shows/hides a menu when a button is cicked $(‘#main-menu

  • 0

I have this code which shows/hides a menu when a button is cicked

$('#main-menu ul').slideToggle();

the menu is hidden when the browser window is less than 767 px, so this button appears which allows it to be toggled on and off, this works except the only problem is when I resize the window bigger again, the menu is gone, if slide toggled has set it to display none. to fix this I added code to show the menu once the browser resizes. this then causes the new menu to show, when the browser window is scaled down again less than 767 px, instead of having the default be hidden. I can then hide the menu again when scaled less than 767px but this causes a flash of the menu as it is hidden.

I was wondering a way to detect if resize() was scaling up r down in this case when scaling down I can hide() menu and when scaling up I can show() menu.

$('.menu-button').click(function() {
    $('#main-menu ul').slideToggle();
});



    $(window).resize(function(){
                var w = $(window).width();
                if(w > 767 && $('#main-menu ul').is(':hidden')) {
                    $('#main-menu ul').show();
                }
            });




    $(window).resize(function(){
                var w = $(window).width();
                if(w < 767) {
                    $('#main-menu ul').hide();
                }
            });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:00:33+00:00Added an answer on June 14, 2026 at 12:00 am

    You’ll need to store the previous dimensions and decide whether the resize is increasing or decreasing in size based on that.

    jQuery(function($) {
        // Cache a reference to $(window), for performance, and get the initial dimensions of the window
        var $window = $(window),
            previousDimensions = {
                width: $window.width(),
                height: $window.height()
            };
    
        $window.resize(function(e) {
            var newDimensions = {
                width: $window.width(),
                height: $window.height()
            };
    
            if (newDimensions.width > previousDimensions.width) {
                // scaling up
            } else {
                // scaling down
            }
    
            // Store the new dimensions
            previousDimensions = newDimensions;
        });
    });
    

    To keep the menu visible when the browser window is resized and the user’s chosen to display it, you’ll just need to store a flag stating that the user chose to make it visible:

    $('.menu-toggle-button').click(function(e) {
        var $menu = $('#main-menu ul');
    
        if (!$menu.data('keepVisible')) {
            $menu
                .data('keepVisible', true); // Store the user's choice (show the menu)
                .slideDown(); // Show the menu
        } else {
            $menu
                .data('keepVisible', false); // Store the user's choice (hide the menu)
                .slideUp(); // Hide the menu
        }
    });
    
    $(window).resize(function() {
        var $menu = $('#main-menu ul');
    
        if ($(window).width() < 767) {
            // Window is smaller than 767 pixels wide
            if (!$menu.data('keepVisible')) {
                // Hide the menu if it hasn't been kept visible by the user
                $menu.hide();
            }
        } else if ($menu.is(':hidden') || $menu.data('keepVisible')) {
            Window is larger than 767 pixels wide and the menu is invisible OR has been shown manually by the user
            $menu
                .data('keepVisible', false) // Reset the user's choice
                .show(); // Show the menu
        }
    );
    

    (Note that with this technique, the code block that determines if the window got bigger or smaller is completely unnecessary)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which does the trick: #include <stdio.h> int main() { int
I have the following code which shows nested ul and hides the other open
I have this code, which shows the tooltip within a box with the id
I have this code, which adds a extra table row when clicked and hides
I have this code which adds a x number of views to a Multiview
I have this code which is called at an onChange event of an function
I have this code which gets WP posts, but it gets all the posts
I have this code (which is way simplified from the real code): public interface
I have this code which compiles and works as expected: class Right {}; class
I have this code which will include template.php file from inside each of these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.