Animate the entrance of mobile content as it slides into view while scrolling

I am currently working on a WordPress theme and facing an issue:

When I click the hamburger menu without scrolling down the page, the mobile menu slides in smoothly. However, if I scroll down the page, the mobile menu fails to slide into view (I have to scroll back up to see it).

I've experimented with various solutions like setting overflow-y to scroll, but nothing seems to work! Do you recommend using JavaScript?

Here's the HTML/PHP code snippet:

<nav class="navbar navbar-fixed-top navbar-light">
        <div class="container">    
                <a class="navbar-brand" href="#">
                    <img src="<?php bloginfo('template_directory');?>/img/fullLogo.png" class="fluid-img pull-md-left" id="navLogo">
                </a>

                <div class="hidden-sm-down">
                    <?php 
                    $mobileMenu = array(
                        'sort_column' => 'menu_order',
                        'container' => false,
                        'theme_location' => 'mobile_menu',
                        'menu_class' => 'desktop-nav',
                        'container_id' => 'desktopNavContainer', 
                    );
                    wp_nav_menu( $mobileMenu );
                    ?>
                </div>

                <a>
                    <button type="button" id="hamburger" class="pull-right hidden-md-up">
                        &#9776;
                    </button>
                </a>
        </div>
        </nav>
    <div id="mobileMenu">
        <button type="button" id="mobileMenuCloseButton" class="pull-right">
            &#735;
        </button>
        <div id="mobileNavWrapper">
            <?php 
                $mobileMenu = array(
                    'sort_column' => 'menu_order',
                    'container' => false,
                    'theme_location' => 'mobile_menu',
                    'menu_class' => 'mobile-menu',
                    'container_id' => 'mobileNavContainer', 
                );
                wp_nav_menu( $mobileMenu );
            ?> 
        </div>
    </div>

The CSS styling for navigation components:

/******** NAVIGATION ************/

#mobileMenu{
    display: none;
    position: relative;
    z-index: 10000;
    top: 0;
    background-color: rgba(0,0,0,.95);
    width: 100%;
    font-size: 3em;
    text-align: center;
    overflow-y: scroll;
}

#mobileNavWrapper{
    margin-bottom: 70%;
}

#mobileMenuCloseButton{
    font-size: 2em;
    color: white;
    position: absolute;
    top: -25px;
    right: 9%;
}

Lastly, let's take a look at the JS implementation:

/* ----------- MOBILE MENU APPEAR --------------- */
    $('#hamburger').click(function(){
        console.log('got it');
        $('#mobileMenu').slideDown(400);
    });

    $(window).scroll(function(){
        if ($(window).scrollTop() > 600){
            $('#mobileNav').addClass('menuScrolled');
        } else{
            $('#mobileNav').removeClass('menuScrolled');
        }
                     });

Answer №1

Hey there, check out this quick mock up I put together for a fixed header to show you how the navbar sticks to the top.

Here's the HTML:

<nav class="mobile-header">
      <ul>
        <!-- Keeping this empty for now --> 
      </ul>
      <div class="hamburger">
        <i class="fa fa-bars fa-2x"></i>
      </div>
    </nav>

And here's the CSS:

.mobile-header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 75px;
  background-color: rgba(0, 0, 0, 0.8);
}

.hamburger {
  position: absolute;
  top: 20px;
  right: 20px;
}

Check out the code in action here!

Take a look at how it works ^

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

using javascript to dynamically color rows in a table based on the value in the status field

Hi, I'm new to javascript and seeking help for my table coloring issue. I have a dynamic table where data is inserted through an Ajax call. The goal is to color specific records based on their status. I attempted a solution but only one record gets c ...

Modify top position without affecting bottom alignment using CSS

I am currently working on a website that is designed to mimic a printable document. The layout consists of a header, body, and footer, with each element utilizing CSS for margin and height adjustments. The challenge lies in ensuring that the footer is alw ...

Switch between various components using multiple buttons in Next.js

I am in the process of creating a component that will display different components depending on which button the user selects. Here are the available “pages”: ` const navigation = [ { name: "EOQ", return: "<EOQgraph />" }, { nam ...

Issues with Linear-Gradient functionality in NativeScript 8 on Android devices

I recently added a linear-gradient to an image in my NativeScript 8 app. Surprisingly, it seems to work perfectly on iOS, but I'm encountering some issues on Android. Despite trying solutions like using -webkit-linear-gradient(), the desired effect is ...

How come a child element with a lower z-index than its parent doesn't appear behind the parent?

I'm currently investigating why a child element fails to appear behind its parent element when it has a lower z-index value than the parent. The issue seems to arise only when the default z-index value of the parent is modified. Let's consider a ...

Changes in an image element's transition can result in either resizing or shifting positions

When I apply an opacity transition to an img element as shown here, I notice that the size of the image changes or it appears to move at the start and end of the transition. Below is a simple CSS code for styling: img{ height:165px; width:165px; ...

Layering an object on top of a clone using CSS

I am working on a webpage with a jQuery menu located at the following link: Menu To accommodate more items and have greater control, I duplicated the menu twice. However, when hovering over them, the duplication is visible in the background. Is there a ...

The value within the style.setProperty function does not get applied

I have a progress bar that dynamically increases based on user input: <div class="progressBarContainer percentBar"> <div class="progressBarPercent" style="--width:${gPercent}" id="${gName}-pbar">< ...

Is there a way I can turn off the dragging functionality in my situation?

Is there a method to disable the dragging functionality within a draggable element? $('#dragitem').draggable({ scroll : false, drag: function(event, ui){ //check if condition is met if( ...

The xModal window will only pop up once, after which a page refresh is required

My modal window opens when a user clicks on a div, but I'm having an issue. The modal window doesn't reopen when I click on the div again. Here is my code: <div onclick="document.getElementById('id01').style.display='block&apos ...

Troubleshooting CSS Issues in ASP.NET Boilerplate

I am attempting to apply a CSS style to a text input similar to the one found on the CreateUser default page. However, I am encountering an issue where the blue line under the textbox does not appear when I navigate away from and then return to the page. ...

The Javascript navigation bar is not displaying properly on mobile devices as it should

After customizing the responsive navbar tutorial from W3Schools using CSS and JS, I encountered an issue. I wanted to include a logo and a web page title before the navbar, which worked perfectly when the webpage was maximized. However, when I resized the ...

Stacking elements in perfect alignment

Seeking assistance with React/MUI as a newcomer. I am utilizing a Stack of TextFields, which are resizing effectively based on screen width. <Stack direction="row" sx={{ margin: "16px" }} > ...

Navigating the jQuery Search Form: Redirecting to Pages within your Website

After successfully creating my first Javascript search form with an Autocomplete Script, I am facing a challenge. I want to enable users to press "enter" after searching for a product and get redirected to the corresponding product URL page. The operation ...

Error alert: The $ symbol is not defined

I followed a tutorial to create an auto-advancing slideshow in JavaScript/jQuery and it worked perfectly on jsfiddle. However, when I transferred everything to Dreamweaver, it stopped functioning. I have triple-checked that all the necessary files are link ...

Having trouble getting my soundboard to work properly on mobile devices

I recently created a soundboard using HTML5, CSS3, and JavaScript. While the audio plays back perfectly on my computer when I click the buttons, I encounter an issue when trying to use it on a smartphone. <!DOCTYPE html> <html lang="en"> <h ...

Mobile device causing HTML margins to shrink

I've attempted to utilize a few posts here that discuss resizing for mobile devices, but unfortunately, I'm having trouble getting it to work correctly. The margins are causing all the elements to be squished together when viewing the site on a m ...

Displaying and hiding the top menu when the div is scrolled

I have developed a script that is designed to display the menu in a shaking motion and hide it as you scroll down. It functions properly when scrolling within the body of the webpage, but I am facing issues when attempting to do so with a div that has an o ...

Center the text vertically within a card using Vuetify styling

I am seeking a way to vertically align text within a card using Vuetify or traditional CSS in a Vue project. Here is my code: <template> <div> <v-container class="my-5"> <v-row justify="space-between"> <v- ...

Adjusting the Underline Navigation Effect with jQuery

My current setup includes a basic navbar with an up arrow that slides underneath it when a navigation title is clicked. The arrow initially rests between two nav titles, and when one of them is clicked, some text also slides in. I am looking to implement ...