When I adjust the window size, the media queries cause the responsive side menu to quickly show and hide

After creating a responsive side menu that automatically appears when resizing the browser window, I encountered an unexpected issue. The side menu seems to appear and disappear quickly without clicking on the designated button. This particular problem is causing some inconvenience for me while everything else functions perfectly.

I am seeking assistance to determine the root cause of this issue. Can anyone provide insight into what might be causing this behavior?

var btn = document.querySelector(".toggle-btn");
var nav = document.querySelector(".nav");

btn.addEventListener("click", function () {
    btn.classList.toggle("nav-open");
    nav.classList.toggle("nav-open");
});
@media screen and (max-width: 1300px) {

    html,
    body {
        margin: 0px;
        padding: 0px;
    }

    .toggle-btn {
        height: 30px;
        width: 30px;
        position: relative;
        float: right;
        top: 10px;
        right: 20px;
        cursor: pointer;
        z-index: 1000;
    }

    .toggle-btn span {
        height: 3px;
        background-color: #2c3e50;
        width: 100%;
        position: absolute;
        top: 20px;
        left: 0;
        transition: .4s;
    }

    .toggle-btn span:before {
        content: '';
        height: 3px;
        background-color: #2c3e50;
        width: 100%;
        position: absolute;
        top: -10px;
        left: 0;
        transition: .4s;
    }

    .toggle-btn span:after {
        content: '';
        height: 3px;
        background-color: #2c3e50;
        width: 100%;
        position: absolute;
        top: 10px;
        left: 0;
        transition: .4s;
    }

    .menu {
        height: 1000px;
        background-color: #eeebec;
        width: 250px;
        position: absolute;
        right: 0px;
    }

    .menu a {
        color: text-align: center;
        display: block;
        padding-top: 30px;
    }

    .nav {
        margin-right: -250px;
        transition-duration: 0.4s;
    }

    .nav-open {
        margin-right: 0px;
        transition-duration: 0.4s;
    }

    .toggle-btn.nav-open span {
        background: rgba(0, 0, 0, 0);
    }

    .toggle-btn.nav-open span:before {
        top: 0;
        transform: rotate(45deg);
    }

    .toggle-btn.nav-open span:after {
        top: 0;
        transform: rotate(-45deg);
    }
}
<!DOCTYPE html>
<html lang="fr">

<head>
    <meta charset="UTF-8>;
    <title>Toggle Button</title>
    <link rel="stylesheet" href="btn.css">
</head>

<body>
    <div class="toggle-btn">
        <span></span>
    </div>

    <div class="menu nav">
        Blah
        Blah
        Blah
    </div>

    <script src="btn.js"></script>
</body>

</html>

    transform: rotate(45deg);
}

.toggle-btn.nav-open span:after {
    top: 0;
    transform: rotate(-45deg);
}

}

Answer №1

It appears that the FOUC (flash of unstyled content) is triggered by how the menu is positioned within the menu class, along with the subsequent negative margin specified in the .nav class.

Consider relocating the negative margin to the .menu definition:

.menu {
    height: 1000px;
    background-color: #eeebec;
    width: 250px;
    position: absolute;
    right: 0px;
    margin-right: -250px; /* <---- */
}

Alternatively, you could rearrange the order of the .nav and menu definitions. (Though it may be more efficient to use a single class name for styling the same element.)

Answer №2

The delay of .4s is triggered by the transition. To eliminate the delay, simply remove the transition from the nav element.

.nav { margin-right: -250px; transition-duration: 0.4s; //make sure to delete this line }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues encountered with java applet functionality when incorporated into an HTML document

I'm facing a simple issue with my "Hello World" applet not converting into HTML as expected. Even though I have provided the necessary code and base to upload the file, the HTML is showing an error that resembles commands from an operating system ter ...

Bootstrap dropdown feature automatically rearranges all items in my navigation bar

I recently set up a navbar for my website, but I'm struggling to grasp the concept of positioning items with Bootstrap. One issue I encountered is that when I click on the dropdown button (profile image), it causes all the items in my navbar to shift, ...

A pause of 5 seconds between every request in Node.js

Need Help with Delays in Node.js Request Queue I am facing an issue with my code that involves looping through more than 500,000 records in a database and requesting information from another server. I have managed to write all the necessary functions, but ...

Expanding the navigation bar causes it to reach the second row after being translated

I am currently working on a website that will be dynamically displayed in both English and Spanish. One issue I have encountered is that the longer words in Spanish are causing the navigation bar to spill onto a second row. For more details, please visit: ...

What is the best way to ensure that the input submit element is only visible after the input checkbox element has been checked?

For a school project, I am attempting to create a simulated reCAPTCHA box. Although my code is complete, I would like the functionality to submit your response from the input fields above only after selecting the reCAPTCHA checkbox. Upon researching, it ap ...

Is it possible to switch between Jquery and CSS?

Using PHP, I have created a CSS file that enables me to easily adjust the color of various elements. Is there a way for me to regenerate the stylesheet and apply it to the DOM using JQuery? For example: <?php if (isset($_POST['mycolor'])){ $ ...

Executing a jQuery event after a div's background-image has finished rendering

Greetings and thank you for sparing a moment. I'm curious to know if there exists a method through jQuery to execute a function immediately after a background image in a div has completed downloading and rendering. Imagine you have the following div ...

Utilizing Jquery on the client side in conjunction with a Node.js server

I am using nodejs within a docker-compose container to create a local web app. My goal is to use jquery on the client-side to load an HTML file into a div, but I'm encountering issues with it not working properly. Both the initial index.html and the n ...

Is it possible to create a fading effect on an image using a border-radius of 50%?

Struggling with fading out an image that's circular in shape due to the border-radius: 50%. <section id="main"> <h1>Non-Important-Text</h1> <h4> it's all fun and games until you can't find a damn sol ...

Embracing unconventional attributes within AngularJS

After converting my model from Java to JavaScript using GWT 2.7, I have a set of properties with getValue() and setValue() methods that are not obfuscated. I am interested in using these properties in {{}} expressions, especially for data binding with ngM ...

"Embed a filter in the loopback framework prior to sending a GET request

I've integrated Loopback into my Node.js app for generating the CRUD API automatically. I needed to customize one of the GET APIs to include a table as well. The typical way to achieve this is by adding an include filter in the query like: /api/expen ...

Encountering difficulty with installing node_modules on Express js

Encountered an error while attempting to install node_modules from the package.json file. npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class="__cf_em ...

How can I pass an Angular parameter to an onclick function?

Let's dive in... I am dealing with a controller $scope.selectedScript = {}; $scope.selectedScript.scriptId = null; $scope.selectScript = function(script, index) { $scope.selectedScript = script; ...

Creating custom fonts using compass

Apologies if my English is not up to par, as I am French. I'm encountering an issue where I include a font-face in my scss file like so: @include font-face("Enhanced Dot Digital", font-files("font/enhanced_dot_digital.ttf")); However, upon compilati ...

Assist me in minimizing redundant code in a basic jQuery function

I successfully created a carousel using the jQuery cycle plugin with 4 links that correspond to different slides. Currently, I have separate blocks of code for each link, but I'm looking to optimize by creating a single function. $('#features-sl ...

Capturing and transfering content from a designated div element to a textarea

Looking to enhance user experience by dynamically adding text to a textarea upon clicking an edit link. Once the edit link is clicked, a pop-up box displays a textarea with the current comment for the user. Multiple .comment-block instances will exist con ...

Transmit data from an HTML form to PHP using JSON

I am attempting to utilize JavaScript to send POST data. I have the data in an HTML form: <form name="messageact" action=""> <input name="name" type="text" id="username" size="15" /> <input name="massage" type="text" id="usermsg" si ...

Aligning the menu links to the right on a horizontal CSS menu

I have created a horizontal menu using CSS and HTML with the following code: .nav > li:hover { background: #666666; text-decoration:none; } .active { background: #666666; text-decoration:none; } nav, ul, li, a { margin: 0; paddi ...

Working with Ember.js to manage relationships using a select element (dropdown) for create/edit operations

I'm trying to establish a relationship through a dropdown in my Ember application. Here is my Books model: import DS from 'ember-data'; export default DS.Model.extend({ // Relationships author: DS.belongsTo('author'), name ...