Utilizing Conditional Logic to Create a Dynamic Menu

I have a navigation menu on my website that is divided into two sections.

There is a left panel and a right panel which slide in from the side when their respective buttons are clicked, covering the browser window.

The functionality of sliding in the panels is working fine for me at the moment.

However, I am struggling with creating conditional logic for the function that checks:

  • 'If the left panel is 'active' / 'visible' and the right panel button is clicked, slide out the left panel while sliding in the right panel.'

and vice versa.

You can find the code snippet here and below is the actual code:

NOTE: I have attempted to trigger an alert when the class name .panel--oneBtn.is-active is present but no alert is being displayed. Thank you

HTML

<div class="wrapper">
  <div class="nav_header">

    <div class='nav_header_link_wrap abso-left panel--oneBtn'>
      <span class="nav_header_link abso-left open">Eat.Drink.Sleep</span>
      <span class="nav_header_link abso-left close">Close</span>
    </div>
    <div class='nav_header_link_wrap abso-right panel--twoBtn'>
      <span class="nav_header_link abso-right open">Bookings/Contact</span>
      <span class="nav_header_link abso-right close">Close</span>
    </div>

 </div>

 <div class="menuPanel panel--one"></div>
 <div class="menuPanel panel--two"></div>

</div>

JS

function navSlider(){
  var $buttonOne = $('.panel--oneBtn'),
    $buttonTwo = $('.panel--twoBtn');

  $buttonOne.on('click', function() {
    $('.panel--one').toggleClass('is-visible');
    $buttonOne.find('.open').toggleClass('is-gone');
    $buttonOne.find('.close').toggleClass('is-visible');
    $(this).toggleClass('is-active');
  });

  $buttonTwo.on('click', function() {
    $('.panel--two').toggleClass('is-visible');
    $buttonTwo.find('.open').toggleClass('is-gone');
    $buttonTwo.find('.close').toggleClass('is-visible');
    $(this).toggleClass('is-active');
  });

  if ( $buttonOne.hasClass('is-active') ){
    alert('Left is active');
  } else if ($buttonTwo.hasClass('is-active') ){
    alert('Right is active');
  }

}

navSlider();

Answer №1

Does this solution meet your requirements?

https://jsfiddle.net/johndoe/abcd1234/99/

$btnPrimary.on('click', function() {

  // New code block
    if ( $btnSecondary.hasClass('is-active') ) {
        $('.panel--secondary').toggleClass('is-visible');
        $btnSecondary.find('.open').toggleClass('is-gone');
        $btnSecondary.find('.close').toggleClass('is-visible');
        $btnSecondary.toggleClass('is-active');
    }
  //

    $('.panel--primary').toggleClass('is-visible');
    $btnPrimary.find('.open').toggleClass('is-gone');
    $btnPrimary.find('.close').toggleClass('is-visible');
    $(this).toggleClass('is-active');
});



$btnSecondary.on('click', function() {

  //New code block
    if ( $btnPrimary.hasClass('is-active') ) {
        $('.panel--primary').toggleClass('is-visible');
        $btnPrimary.find('.open').toggleClass('is-gone');
        $btnPrimary.find('.close').toggleClass('is-visible');
        $btnPrimary.toggleClass('is-active');
    }
  //

    $('.panel--secondary').toggleClass('is-visible');
    $btnSecondary.find('.open').toggleClass('is-gone');
    $btnSecondary.find('.close').toggleClass('is-visible');
    $(this).toggleClass('is-active');
});

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

How can JavaScript be used to delete cache and cookies?

Looking for a solution to clear cache and cookies via JavaScript? I'm currently using Selenium for testing in Microsoft Edge, but the tests are running in the same session as the previous run. I need each test to run in a clean session every time. Unf ...

The dual-file upload feature of the jQuery ajaxForm plugin

After extensive searching online, I have yet to find an answer to my problem. The issue at hand is that when using the ajaxForm malsup plugin, it submits my files twice. HTML: <form id="gallery" enctype="multipart/form-data" action="/upload-gallery.p ...

Vue event manager, accessible for all components

I have created a new Vue instance and assigned it to the window object, thinking that it would be accessible throughout all components. I expected this setup to allow me access to events emitted anywhere within my application. However, it seems like this ...

Getting the value from a .sh (Shell Script) file in React: How to do it?

There is a .sh file that contains the following code: echo "Hello" This code produces the output: Hello The question at hand is: I am trying to extract the output from the .sh file and integrate it into my React application. After exploring various ...

Using Ajax and PHP to upload an image

I'm looking to implement an image upload feature triggered by a button click with the id of #myid.save. Below is the code I have so far: HTML Code: <canvas id="cnv" width="500" height="100"></canvas> <input id="myid_save" type="submit ...

Tips for concealing JavaScript code while inspecting elements with AngularJS

Is it possible to display minified JavaScript code when using Angular and inspecting elements? ...

Tips for monitoring password and password confirmation fields in DevTools when using AngularJS forms

Within the ng-model, I have identified two variables: vm.user.password and vm.confirmpassword. The input fields are equipped with attributes such as id, name, and class. I am interested in monitoring both the password and confirmpassword values within Dev ...

Determine the position and quantity of elements in jQuery by comparing their IDs with the current page or element

Looking to retrieve the n (zero based) position of an element by matching the page and element ID... Let's use an example (Assume the current page ID is 488); <ul id="work-grid"> <li id="item-486" class="work-item"><!--/content--& ...

The Gulpfile.js encountered an error while trying to load

My Visual Studio task runner is having trouble loading the gulp file. I am currently using VS2017 v15.9.4, but the project was developed some years ago. Failed to run "...\Gulpfile.js"... cmd.exe /c gulp --tasks-simple assert.js:350 throw err; ...

Obtain CSS attributes for utilization in Rails variables and database operations

Whenever I click on multiple divs labeled as scale-up, I am able to acquire the css attribute. Yet, my goal is to save these imported css properties in Rails variables and store them in the database. Ultimately, what I aim for is to retrieve the Css prop ...

Adding scripts to a PartialView in MVC using Jquery's append function

My issue is with a JavaScript function that appends a PartialView into a div. Everything seems to work correctly, except for the fact that the PartialView contains some scripts enclosed within <script></script> tags. As a result, when these dyn ...

A guide on incorporating an event to multiple elements in vue.js

I am trying to implement a double-click event on multiple elements in Vue3. My goal is to create a table of contents data and add a double-click event to the checkboxes and favorite icons without using @dblclick="". I find it more convenient to assign a c ...

Troubleshooting issues with Three.js and .obj file shadows

I've been diving into learning Thee.js, and while it's fairly straightforward, I've hit a roadblock with getting shadows to work. Despite setting castShadows, recieveShadows, and shadowMapEnabled to true in the appropriate places, shadows ar ...

Utilizing LoopBack Storage Service: Leveraging upload/download functions within JavaScript code

Is there a straightforward way to upload and download files using the storageService.upload and storageService.download functions in my JavaScript/Loopback code? I'm trying to achieve something like this: app.post("/sendFile", (req, res) => client ...

What are the essential Angular 2 observables for me to utilize?

I have created a form input for conducting searches. Upon user input into the search bar, I want to trigger calls to two separate APIs simultaneously. Here is my current attempt: myInput: new FormControl(); listOne: Observable<string[]>; listTwo: Ob ...

What are the steps for implementing a equirectangular image in WebXR with three.js?

Currently, I am developing a web application with Three.js that requires the use of equirectangular images for panoramic tours. My goal is to incorporate a feature that allows users to switch between normal mode and VR mode similar to React360 and AFrame. ...

Is it possible to establish a limit on a field's value in MongoDB?

Just a quick query - I'm curious if there's a feature in mongodb that allows for fields to have a set maximum value. For instance, let's say the field "cards" is restricted to a maximum value of 100. If an increment would exceed this limit, ...

Employing a function to concatenate promises

In my coding process, I have come across a situation where I need to fetch content and then save it using two separate functions. Each function performs a different task based on the type of content provided. These functions act as helper functions in my o ...

Problems with Angular UI Router: Functions not functioning properly within the template

Currently, I am delving into the realm of Angular UI Router in order to effectively manage different sections of my application. If you'd like, check out this plunkr (http://plnkr.co/edit/KH7lgNOG7iCAEDS2D3C1?p=preview) Here are some issues that I&a ...

Unable to access this context in Firefox debugger after promise is returned

I'm curious as to why the 'this' object is not showing up in the debugger in Firefox, but it does appear in Chrome's debugger. When I try to access 'this.myProperty', it shows as undefined. This is the code from my TypeScript ...