Problem with full-page navigation sliding in and fading in and out

Upon the user's click on

<a href="#slide-nav" class="slide-nav-trigger">
, a full-page navigation smoothly slides into view. This animation is triggered by CSS and uses jQuery for event delegation.

The Dilemma

Instead of abruptly turning on and off when toggling the navigation-is-on class to the body, I aim to have slide-nav fade in and out. By animating the opacity of slide-nav, I successfully achieved a fading effect upon one click.

$(function(){
// assign click event to slide-nav-trigger
$('.slide-nav-trigger').on('click',function(event){
    event.preventDefault(); // cancel the default action

    // toggle navigation-is-open class to body upon click
    $('body').toggleClass('navigation-is-open');

    var slideNav = $('.slide-nav');
    slideNav.animate({
        opacity:1 }, 2000, function(){});
    });
});

In this code snippet, I'm specifically targeting slide-nav and animating its opacity to create the desired fading effect. Despite trying to utilize the fadeIn method, I found that this approach remains the most effective in achieving my intended outcome.

To explore further, visit this CodePen link.

Answer №1

For the functionality of running another function on "click" if the body has a certain class, you will need to use additional JavaScript. However, you can achieve similar effects using only CSS by leveraging the existing class trigger:

Check out this CodePen example

.slide-nav{
    position: fixed;
    z-index: 1;
    top:0;
    left:0;
    height:100%;
    width:100%;
    opacity: 0;
    display: block;
    background-color:#51585A;
        transition: opacity 2s ease-in-out;
    .navigation-is-open & {
        opacity: 1;
    }
    .slide-navigation-wrapper{
        // main navigation content here
        height:100%;
        overflow-y: auto;
        overflow-scrolling:touch;
        padding:40px 5% 40px calc(5% + 80px); /* padds links over to the left */
        @include transition(translateZ(0)); /* Force Hardware Acceleration for Webkit */
        backface-visibility:hidden;
        @include transform(translateX(-50%)); /* Transform links off screen on X axis by -50% */
        @include transition(transform $animation-dur);
        @include transition-timing-function(cubic-bezier(.86,.01,.77,.78)); /* Smooth animation effect */
    }
    
    .navigation-is-open & { /* When navigation is open, apply styles */
        visibility: visible;
        
        .slide-navigation-wrapper{ /* Slide out navigation wrapper when open */
            @include transform(translateX(0)); /* Transition translate on the X-axis to 0 */
        }
    }
}

Answer №2

Implemented a solution to fade in and out the opacity of the slide-nav div using jQuery.

Upon clicking the slide-nav-trigger, the navigation-is-open class is toggled on the body element. This triggers the fade in animation on the slide-nav as the first evaluation in the if statement returns true.

// Check if body has class 'navigation-is-open'
if ($('body').hasClass('navigation-is-open')){
        // Animate slide-nav opacity from 0 to 1
        slideNav.animate({
            opacity: 1 }, 2000, function(){});
       }

If the user clicks on slide-nav-trigger again, the navigation-is-open class is removed from the body. This causes the evaluation in the if statement to be false, triggering the fade out animation on the slide-nav.

if ($('body').hasClass('navigation-is-open')){
    // Animate slide-nav opacity from 0 to 1
    slideNav.animate({
        opacity: 1 }, 2000, function(){});
    } else {
        // Animate slide-nav opacity from 1 to 0
        slideNav.animate({
            opacity: 0 }, 1000, function(){});
    }

Updated Pen

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

Utilizing the power of moment.js within an Angular component

My goal is to utilize the moment() function within double curly braces {{}} in order to present a date: {{ moment().date(timeslot.start.value.month) .month(timeslot.start.value.dayOfMonth - 1) .format("MMMM Do") }} Unfortunately, I ...

Is there a way to achieve this without relying on Ext.getCmp in this particular code snippet?

I am currently using the following code to trigger a button click event once the window show event is called. It works perfectly fine, but I want to achieve the same functionality without using Ext.getCmp. Here is the line of code: Ext.getCmp('recen ...

What is the method for returning a string array?

My query is about how to return a string[]. Currently, TypeScript is throwing an error because each element of the array has a type of ( T[keyof T] extends readonly (infer InnerArr)[] ? InnerArr : T[keyof T] ). How can I accept the 'property' arg ...

Is the utilization of react-router for developing 'single-page applications' causing a depletion of server resources?

Hello, I am relatively new to the world of web development and would appreciate guidance on any misunderstandings I may have. Currently, I am immersing myself in learning the MERN stack. For my practice project, I am aiming to create a simple two-page webs ...

One Background Image Serving Multiple Divs

Can you use one image (PNG or SVG) as the background for multiple divs? Take a look at the images below to see how it could work. And if the screen width gets smaller and the divs stack up vertically, is there a way to change the background accordingly? D ...

Tips on altering the h2 color when hovering using h2:hover:after

I'm attempting to alter the color of my h2 element using h2:hover:after. Can someone guide me on how to achieve this? Here is what I have tried so far. h2 { font-size: 25px; } h2:after { content: ""; display: block; width: 14%; padding-to ...

Saving data from Material UI forms in TypeScript

Is there an effective method for storing values entered into the text fields on this page? const AddUserPage = () => ( <div> <PermanentDrawerLeft></PermanentDrawerLeft> <div className='main-content'> < ...

A guide on navigating to a different component in Vuejs using a link

<div class="enterprise-details" style="margin-top: 20px"> Already signed up? <a href="#"> LOGIN</a></div> <!-- Component for redirection --> <b-button v-if="!registeredUser" class="button-self" v-b-modal.modal-x>Lo ...

The operation to remove the child element could not be completed

var first_content = document.getElementById('first-content'), offered_games = document.getElementById('offered-games'); for(var i = 0, e = offered_games.children; i < e.length; i++) { e[i].onmouseenter = function() { var ...

No matter how many times I modified the code in the ReactDOM.render() method within my index.js file, the end result remained unchanged

When I ran npx create-react-app my-app, and then proceeded to cd my-app and npm start, a browser opened on localhost:3000. While looking at the index.js file, I noticed that the ReactDOM.render() method included the following code: ReactDOM.render( <Rea ...

Unable to choose an input form within a CSS div

Just installed a jQuery responsive menu tab but encountering issues with selecting forms to input values. It seems like a CSS problem that I cannot identify. Please assist me in resolving this issue. Below is the HTML code snippet: <div class="contai ...

New feature: Material UI Chip Input with floating input label

I installed material-ui-chip-input to implement tags in an input field. I wanted a label alongside it, so I utilized the default Material UI label for consistency with other inputs. However, the input doesn't shrink as expected when clicked on by the ...

the attempt to send an array of data to the $.ajax function was unsuccessful

let myArray = []; myArray.push(someValue); let requestData = { action: myAction, array: myArray }; $.ajax({ type: "POST", data: requestData, url: requestUrl, success: handleSuccess, error: handleError }); When ...

The jQuery call to a web service is returning XML data, but the success function is receiving a

I have utilized: [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] The outcome of my web service call is: <string xmlns="http://tempuri.org/"> [{_pkId:"",_code:"",_message:"The file has been uploaded successfully.",_sta ...

Adding a programmatic option with additional data

I am encountering an issue with my select2 multi-select element that has an ajax data source. Upon loading, I need to automatically preselect options in my select2. Although I have successfully followed the instructions outlined in the documentation for po ...

Is it possible to adjust the width of Material-UI TextField to match the width of the input text?

Is there a way for Material-UI to adjust the width of the TextField element automatically based on the input text? When creating a form view/edit page and rendering data into fields, I also have parameters set by the server. It would be convenient to have ...

Creating a custom event handler for form input changes using React hooks

A unique React hook was created specifically for managing form elements. This hook provides access to the current state of form fields and a factory for generating change handlers. While it works seamlessly with text inputs, there is a need to modify the c ...

getting data from JavaScript function by making an asynchronous request to RESTful API

My current challenge involves calling a JavaScript method that utilizes AJAX to access a REST service and then return the response to the original call. While this may seem straightforward, I have scoured Stack Overflow for answers but haven't found a ...

Is there a way in jQuery to identify if a paragraph contains exactly one hyperlink and no other content?

I need to assign the 'solo' class to a link within a paragraph only if that link is the sole element in the paragraph. This example would have the 'solo' class: <p><a>I am alone</a></p> However, this example w ...

Information sent by the Firefox TCP socket using the socket.send() method cannot be retrieved until the socket is closed

I am experiencing an issue while trying to send data from Firefox to a Java desktop application. My Java class functions as a server, and the Firefox script acts as a client. When I test it using another Java class called client.java, the data is successfu ...