JS will reach its stopping point at the specified style.zIndex

I am currently in the process of setting up button elements. I have two scripts that correspond to different types of buttons - one script runs a simple collapse menu, while the other executes a more complex collapse by shifting depths and sliding one div over another. Everything seems to be functioning correctly except for the depth (zindex) switch. Whenever I implement the depth switch, it stops running anything beyond that point. Below is the specific script causing issues.

/* fetching all menu and page buttons */
var menuButtons = document.getElementsByClassName("menuButton");
var pageButtons = document.getElementsByClassName("pageButton");
/* initializing working array index */
var i;
/* IDs of previous and current content pages */
var prevPage;
var currentPage;
/* when a page button is clicked, set the prevPage to the lowest depth and slide the new page overtop */
for (i = 0; i < pageButtons.length; i++) {
  pageButtons[i].addEventListener("click", function() {
    this.classList.toggle("active");
    /* fetching reference to target page from the button */
    var pageId = this.getAttribute("pageTarget");
    currentPage = document.getElementById(pageId);
    /* setting content page depths */
    prevPage.style.zIndex = "0";
    currentPage.style.zIndex = "1";
    /* setting the left position to start the page transition */
    currentPage.style.left = "100vw";
    alert("button clicked");
    /* triggering a delayed function to clear the previous page */
    clearPrevPage();
  });
};
/* delayed function that transitions prevPage to its starting position */
function clearPrevPage() {
  setTimeout(function() {
    prevPage.style.left = "100vw";
    /* setting the variable for the next run */
    prevPage = currentPage;
    alert("delayed");
  }, 400);
};

Answer №1

Before using prevPage, make sure it is not empty to avoid any errors:

/* accessing menu and page buttons */
var menuButtons = document.getElementsByClassName("menuButton");
var pageButtons = document.getElementsByClassName("pageButton");
/* defining index in array*/
var i;
/* id of previous and current content pages */
var prevPage;
var currentPage;
/* when a page button is clicked, set prevPage to lowest depth and animate new page over it */
for (i = 0; i < pageButtons.length; i++) {
  pageButtons[i].addEventListener("click", function() {
    this.classList.toggle("active");
    /* getting the target page of the button */
    var pageId = this.getAttribute("pageTarget");
    currentPage = document.getElementById(pageId);
    /* setting depths for content pages */
    if (prevPage) prevPage.style.zIndex = "0";
    currentPage.style.zIndex = "1";
    /* starting page transition by setting left position */
    currentPage.style.left = "100vw";
    alert("button clicked");
    /* calling delayed function to clear previous page */
    clearPrevPage();
  });
};
/* function to transition prevPage back to starting position after delay */
function clearPrevPage() {
  setTimeout(function() {
    if (prevPage) prevPage.style.left = "100vw";
    /* updating prevPage for next run */
    prevPage = currentPage;
    alert("delayed");
  }, 400);
};
<button class="menuButton">menu</button>
<button class="pageButton" pageTarget="page1">page</button>
<div id="page1">page1</div>

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

Enhancing features with jQuery's .animate() function

Can anyone help me figure out why the div on the right is not pushing itself away from the one on the left when I hover over it? I want it to move on mouseenter and return to its original position on mouseleave. The changing background colors are just ther ...

Enable Parse5's case sensitivity

Recently, I've attempted to parse Angular Templates into AST using the powerful parse5 library. It seemed like the perfect solution, until I encountered an issue - while parsing HTML, it appears that the library transforms everything to lowercase. Fo ...

Knitting: exporting to HTML file while retaining formatting

Recently discovered the amazing knitr library in R, which looks great when viewed in the viewer. But unfortunately, the style gets lost when saved to an html file. Code library(knitr) library(kableExtra) some.table <- data.frame ( x = rep(1 ...

Preventing page refresh with Javascript when clicking on CAPTCHA

I have been exploring a CAPTCHA tutorial on TutsPlus.com and I am facing an issue where the 'Incorrect Captcha message' keeps appearing, indicating that my user input through $_POST does not match the stored $_SESSION string. Upon investigating ...

Having trouble displaying child nodes in MatTreeView with Angular 14?

In an Angular project, I am attempting to display a private group's data from GitLab (utilizing a public one for testing purposes). To achieve this, I have implemented the NestedTreeView component. While the parent nodes are displaying correctly, I am ...

Attempting to implement an EventListener to alter the navbar upon scrolling, unsuccessful at the moment

Exploring ways to update the navigation bar upon scrolling to shrink its size and modify the color scheme (specifically, transitioning from a transparent background to white and altering font colors). Below is the HTML snippet: /* Defining the overa ...

Issue encountered while retrieving information from XML file through AJAX

I am facing an issue while using AJAX to fetch data from an external XML file. The error I receive is "Invalid argument" and I am working with IE 8. Below is the code snippet: var xhr; xhr = new XMLHttpRequest(); xhr.open("GET","C:/Users/abc/Desktop/Pr ...

I am attempting to transfer information from one page to another in Next.js, but unfortunately, I am experiencing difficulties in

Encountering the following error message: "Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead." Any assistance will be greatly appreciated. export default func ...

Having trouble setting up the next-auth login page and experiencing issues with the getProviders() function

Greetings to all fellow web developers, I am currently working on a Next.js application that utilizes next-auth for user authentication. I have set up the [...nextauth].js file in the "pages/api/auth" directory and a signin.js file in the "pages/auth/" di ...

CSS animation for image hover effect in Revolution Slider

I'm currently facing an issue while using Revolution Slider. To better illustrate, I am referring to the original demo found at this link: . When browsing through the 'portfolio' tab, you'll notice that the images shrink and darken upo ...

Design: Seeking a layout feature where one cell in a row can be larger than the other cells

To better illustrate my goal, refer to this image: Desired Output <\b> Currently, I'm achieving this: current output Imagine 7 rows of data with two columns each. The issue arises in row 1, column 2 where the control needs to span 5 rows v ...

Troubleshooting: Ngx-Echarts Animation Issue at Startup

I've been working on integrating ngx echarts into my Angular app, but I'm facing an issue with the animation during the initial load of the chart. Take a look at this Stackblitz example where you can see that the bars render quickly on initial lo ...

Update the image source every few seconds

I am attempting to show the same image in both gif (animated) and jpeg formats. I am updating the src every few seconds. After checking in the developer tools, it seems that the src has been modified, but the gif does not appear to be animating. setInter ...

What is the best way to incorporate various styles into one :style attribute?

Within my vuetify project, I encountered a challenge of adding multiple styles to the same style attribute. Specifically, I have successfully implemented a vuetify breakpoint and now wish to include {backgroundColor:'Color'} within the existing a ...

Unexpected behavior observed with Async/Await

I am currently learning how to use Async/Await, which is supposed to wait until the Await function finishes executing before moving on with the code. However, I have encountered an issue where my code stops completely after using Await. Here is the method ...

Creating Sub Menu in Blogger with pageListJSON

I am looking to enhance the menu on my blog by adding a sub-menu structure. Specifically, I want to organize my menu to include "Manual Testing" and "Automated Testing" as sub-menus under the main category of "Testing". Testing Manual Testing Automated ...

The React-loadable alert indicated a discrepancy in the text content

Utilizing react-loadable for dynamic JS module loading is part of my process. With server-side rendering already set up and functioning correctly for react-loadable, I am encountering an issue on the client side. Upon page load, a warning message appears i ...

Creating dynamic JSX content in NextJS/JSX without relying on the use of dangerouslySetInnerHTML

I have a string that goes like "Foo #bar baz #fuzz". I'm looking to create a "Caption" component in NextJS where the hashtags become clickable links. Here's my current approach: import Link from "next/link"; const handleHashTag = str => st ...

Error encountered: Invalid symbol detected ('<') while attempting to parse JSON in an Angular application

I am experiencing an issue with my Angular code when trying to submit a form. Upon clicking the submit button, an error saying JSON Parse error: Unrecognized token '<' appears, and empty records are being saved in the database. I have included ...

Challenges encountered with autofill and a null string

When I try to fetch data from the server for autocomplete, it returns no options even though two options are displayed in the console after making an API call. The value I enter includes two empty spaces followed by 'IPH', triggering the API call ...