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

Adding the class ui-corner-bottom to the initial item in a list using JQuery Mobile

After experimenting with JQuery Mobile, I've had some success but now I'm facing a new issue. The first line of my list is automatically adding 'ui-corner-bottom' to the class, which gives the first item in the list bottom corners. Any ...

How can I use AngularJS to save selected assets?

<div style="z-index: 1; position: absolute"ng-show="ctrl.company.selected"> <div style="" ng-repeat="Asset in ctrl.company.selected.Assets"> <div class="pd-5"style="width: 300px; background-color: white; border-bottom: gray solid ...

The issue of isotope overlapping occurs when trying to retrieve items using ajax calls

I've been developing a website and I'm utilizing fullpage.js with two distinct sections. In the second section, I have implemented a link that opens a gallery using ajax when clicked on a specific gallery link. However, after the ajax call, there ...

When utilizing forEach to loop through and interact with elements in React Testing Library, the onClick event handler is not triggered. However, employing a for loop successfully triggers the

In my React project, I've created a functional component called Shop. It accepts a callback function as a prop and displays a list of items. Each item in the list triggers the callback when clicked. function Shop(props) { const { onClickMenu } = p ...

Building a straightforward RESTful API for user authentication with Node.js, MongoDB, and Express.js

Can someone provide guidance on creating a RESTful API using Node.js, Express.js, and MongoDB? Specifically, I am looking for assistance with writing the schema for login and sign up pages, as well as comparing data in MongoDB using Node.js Express.js. As ...

Location of Custom HTML Widget in Django-Dashing

I've encountered a dilemma while using the Django-Dashing framework, specifically regarding the placement of my HTML file for a custom widget. I have meticulously configured the code in my dashboard.html file to ensure proper loading. {% extends &apo ...

Adjust THREE.PerspectiveCamera's distance without altering its viewing orientation

I have a PerspectiveCamera in THREE.js positioned somewhere in space as a child of a mesh. The camera is currently looking at the mesh with local coordinates [0, 0, 0]. I am looking for a way to change the distance of the camera from the mesh without chang ...

The `$scope variable fails to update in another controller`

I am currently facing an issue with updating a value on my view. Let me walk you through my code along with a brief explanation of the situation. The code may look messy as I have been experimenting with different combinations lately. The controller in qu ...

What steps are necessary to activate javascript in HTML for WebView?

I recently discovered that when my HTML/JavaScript site is visited via an Android webview, JavaScript is disabled by default. This causes a pricing list on my page to not display properly because it requires a JavaScript class to be added for it to open. I ...

Blending conditional and non-conditional styles in VueJS

Is it possible to set a class in Vue based on the value of a parameter and conditionally add another class if that parameter meets a certain condition? Can these two functionalities be combined into one class assignment? <button :class="'btn btn-p ...

Encountering a 404 error for a JSONP Get request while deploying on IIS

When utilizing jquery AJAX with datatype jsonp, everything runs smoothly on my local development environment even with a large amount of data. However, once deployed on IIS, it only functions properly for requests shorter than 2121 characters; anything mor ...

The correct way to use the useState hook for implementing an icon in React

My attempt at implementing a feature in React allows users to see active feedback on the screen by changing its icon when clicked. How can I make it change to another icon (e.g. from Box to BoxChecked) once it is clicked? Note: I believe this code line i ...

Ways to retrieve the data from promises after they have been resolved?

I'm struggling to retrieve the values from getPeople(0,4). function getPeople(start, end) { const peopleArray = []; for (let i = start; i <= end; i++) { peopleArray.push( axios.get(`https://www.testsite.net/api/test/workers/ ...

What is the most effective method to prevent postback controls from activating before the page is fully loaded?

My website contains high-quality graphics, which may lead to slow download times for users with poor internet connections. As the browser is still loading, users often access form options and submit their information prematurely. This premature submission ...

Emphasize the active item in the PHP header

Hey there! I've been trying to figure out how to highlight the current page in the header of my website. I'm using the include method to call the header on all pages, but I can't seem to figure out how to highlight the current page while sti ...

What is the best method to determine the mean score by utilizing the ID values obtained from API responses?

These are the responses retrieved from the API: const attractions = [ {"id": 1,"name": "drive on avenue"}, {"id": 2, "name": "diving"}, {"id": 3,"name": "visiting ma ...

When attempting to browse for image files, Postman fails to display images

While trying to upload image files through Postman, I encountered an issue where the browser did not display any image files. It's important to note that I am using Ubuntu as my operating system. When I clicked on "select files," the option appeared ...

The jQuery Validation plugin ensures that a minimum of one image has been uploaded

My current setup includes a form that permits users to upload images. Each image uploaded by the user is shown in a div named imgHolder, located outside the form. In addition, once a user uploads 5 files, the corresponding file[] input from the form is rem ...

How can Vue JS 3 components exchange data between each other?

I am attempting to share data from a variable favorite_count in the Favorites component located in the file Favorites.vue. My goal is to pass this data to the App Component in the App.vue file but I have been unsuccessful so far. I want any changes made to ...

When attempting to import Quill-blot-formatter with react-quill via next/dynamic, the registration process fails and continues to display a loading message

After creating a function component and configuring quill-blot-formatter with react-quill, I added the blotFormatter to the modules list. Then, I imported this module using next/dynamic on the desired page. The custom function looks like this: import Reac ...