Is the z-index feature not functioning as anticipated?

I'm currently working on a project involving a book that flips on click on both sides. The process involves checking the direction when a page is clicked and flipping it to the left if it's not to the right. Additionally, I adjust the z-index to maintain the order of the pages.

However, I've encountered an issue. When I flip the page back to the right from the left, the z-index is applied correctly, but there is a delay in the rendering on the screen. Despite the z-index being updated correctly in the console log, the change is not immediately reflected. I'm unsure of what I might be overlooking. Can anyone provide any insight? Thank you.

const book = document.querySelector('.book');
book.addEventListener('click', e => {
    if (e.target.attributes['data-position'].value !== 'left') {
        e.target.setAttribute('data-position', 'left');
        e.target.classList.remove('flip-to-right');
        e.target.classList.add('flip-to-left');
        setTimeout(() => {
            console.log (e.target.attributes['data-position'].value);
            // e.target.style.zIndex = e.target.attributes['data-index'].value*(-1);
            e.target.style.zIndex = 1;
        }, 1000);
    }
    else {
        e.target.style.zIndex = e.target.attributes['data-index'].value;
        e.target.classList.remove('flip-to-left');
        e.target.classList.add('flip-to-right');
        e.target.setAttribute('data-position', 'right');
    }
})
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

body {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: yellow;
    perspective: 1000px;
}

.book {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.book:hover .cover{
    /* transform: rotateX(10deg) rotateY(-180deg); */
}

.book:hover .page{
    /* transform: rotateX(10deg) rotateY(-180deg); */
    /* z-index: 2; */
}

.flip-to-left {
    transform: rotateX(10deg) rotateY(-180deg);
}

.flip-to-right {
    transform: rotateX(10deg) rotateY(0deg);
}

.cover {
    z-index: 7;
    /* transform: rotateX(10deg) rotateY(-180deg); */
    transition: all 3s;
}

.page-1 {
    z-index: 5;
}
.page-2 {
    z-index: 4;
}
.page-3 {
    z-index: 3;
}
.page-4 {
    z-index: 2;
}
.last-page {
    z-index: 1;
}

.back-cover {
    z-index: 0;
}

.back-cover,
.cover{
    height: 300px;
    width: 260px;
    background-color: blue;
    border-radius: 10px;
    position: absolute;
    box-shadow: 1px 1px 10px gray;
    /* transform: rotateX(10deg); */
    transform-origin: center left;
}

.last-page,
.page {
    height: 280px;
    width: 250px;
    background: white;
    position: absolute;
    border-radius: 10px;
    /* transform: rotateX(10deg); */
    transform-origin: center left;
    transition: all 3s;

    /* z-index: -1; */
}

/* .page:nth-child(2) {
    transition-duration: 3s;
}

.page:nth-child(3) {
    transition-duration: 2.6s;
}
.page:nth-child(4) {
    transition-duration: 2.2s;
}
.page:nth-child(5) {
    transition-duration: 1.8s;
}
.page:nth-child(6) {
    transition-duration: 1.4s;
}

.book:hover .page:nth-child(2) {
    transition-duration: 6s;
}

.book:hover .page:nth-child(2) {
    transition-duration: 6s;
}
.book:hover .page:nth-child(3) {
    transition-duration: 5.6s;
}
.book:hover .page:nth-child(4) {
    transition-duration: 5.2s;
}
.book:hover .page:nth-child(5) {
    transition-duration: 4.8s;
}
.book:hover .page:nth-child(6) {
    transition-duration: 4.4s;
} */

.last-page > div {
    width: 150px;
    border-radius: 10px;
    margin: 20px auto;
    background-color: green;
    position: relative;
    /* z-index: -1; */
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
    <script src="js.js" defer> </script>
</head>
<body>
    <div class="book">
        <div class="cover" data-index="7" data-position="right"> cover </div>
        <div class="page page-1" data-index="6" data-position="right"> page 1</div>
        <div class="page page-2" data-index="5" data-position="right"> page 2</div>
        <div class="page page-3" data-index="4" data-position="right"> page 3</div>
        <div class="page page-4" data-index="3" data-position="right"> page 4</div>
        <div class="last-page" data-index="2" data-position="right">page 5</div>
        <div class="back-cover" data-index="1" data-position="right"></div>
    </div>
</body>
</html>

Answer №1

The issue lies with the CSS property transition: all 3s;. This code is animating all properties, even the zIndex. To fix this, try changing it to transition: transform 3s; in both instances. You may need to include additional properties, but I have not verified this.

Answer №2

Kindly watch the attached video and make the necessary updates to the following CSS before running and testing it.

* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

body {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: yellow;
    perspective: 1000px;
}

.book {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.book:hover .cover{
    /* transform: rotateX(10deg) rotateY(-180deg); */
}

.book:hover .page{
    /* transform: rotateX(10deg) rotateY(-180deg); */
    /* z-index: 2; */
}

.flip-to-left {
    transform: rotateX(10deg) rotateY(-180deg);
}

.flip-to-right {
    transform: rotateX(10deg) rotateY(0deg);
}

.cover {
    z-index: 7;
    /* transform: rotateX(10deg) rotateY(-180deg); */
    transition: all 3s;
}
.cover{
  z-index: 0;
}
.page-1 {
    z-index: 2;
}
.page-2 {
    z-index: 2;
}
.page-3 {
    z-index: 2;
}
.page-4 {
    z-index: 2;
}
.last-page {
    z-index: 2;
}

.back-cover {
    z-index: 3;
}

.back-cover,
.cover{
    height: 300px;
    width: 260px;
    background-color: blue;
    border-radius: 10px;
    position: absolute;
    box-shadow: 1px 1px 10px gray;
    /* transform: rotateX(10deg); */
    transform-origin: center left;
    display: flex;
    align-items: center;
    justify-content: center;
}

.last-page,
.page {
    height: 280px;
    width: 250px;
    background: linear-gradient(272deg, #ffffff, #858181);
    position: absolute;
    border-radius: 10px;
    /* transform: rotateX(10deg); */
    transform-origin: center left;
    transition: all 3s;
    display: flex;
    align-items: center;
    justify-content: center;
}


.last-page > div {
    width: 150px;
    border-radius: 10px;
    margin: 20px auto;
    background-color: green;
    position: relative;
    /* z-index: -1; */
}

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

Optimal approach for vertically aligning elements in CSS

I'm looking to arrange my header ('Sail away today with Starboard Rentals.') and link buttons on top of each other. I want the navigation buttons to be positioned below the h1 on the lower half of the 'home-jumbo' div. What's ...

Are HTML custom data attributes being included in Google's indexing?

Currently, I am in the process of developing a portfolio module that relies on AJAX, pushState, and hash bangs. Since I am targeting browsers with JavaScript enabled, my main concern lies in the limited capability of HTML custom data attributes for SEO. T ...

Having difficulty creating the probot.github app due to an error: The removal of the programmatic API in npm version 8.0.0 causing failure

Currently, I am facing an issue while attempting to set up the probot.github app using the command npx create-probot-app my-first-app which can be found at: . My system is running on the latest node version v19.3.0 with npm version 9.2.0. However, upon exe ...

Tips for making a standout testimonial card?

Apologies for the simplistic inquiry. As a fresh student of a full stack web developer bootcamp, I find myself grappling with this particular challenge. How can I go about creating a testimonial card similar to this one? I've attempted to write code ...

Incorporate information into a JSON structure within SAPUI5

While diving into SAPUI5, I decided to challenge myself by creating a basic form. Unfortunately, my attempts are falling short as the new entry I'm trying to add to my JSON model isn't showing up in the file when I run my code. No error messages ...

Real-time update of quiz results based on varying factors

In my quiz, I have set up variables that start at 0 and increase based on certain conditions. One variable should increase when a question is answered correctly, while the other should increase when a question is answered incorrectly. If you answer a quest ...

Utilizing AJAX in Tables: Implementing an Onclick Function in a Button within a Table to Access JSON Data

I am trying to trigger a function by clicking on a button and passing the position's ID as a parameter, but I am facing some difficulties in understanding how to achieve this with the given syntax. Can anyone guide me through this process? functi ...

Is there a way to use JavaScript or jQuery to automatically convert HTML/CSS files into PDF documents?

While using OS X, I noticed that in Chrome I have the option to Save as PDF in the print window by setting the destination to "Save as PDF". Is this feature available on Windows? Is there a way to easily save to PDF with just one click? How can I access t ...

Fetching the entire webpage

When attempting to load a specific webpage using an iframe, I encountered the following issue: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></ ...

Implementing user profile picture display in express.js: A step-by-step guide

I'm having trouble displaying the profile picture uploaded by users or employees in my application. Although the picture uploads successfully, it doesn't show up and gives an error when I try to redirect to the page. Cannot read property &ap ...

When using the HTML a tag, it can create unexpected gaps and

I have a sentence written in PHP that I've separated into individual word components using the explode function. foreach ($words as $splitword) { echo $splitword; /* echo "<a href = 'word.php?word=" . $splitword . "'>" . $spli ...

The NGX countdown timer is experiencing a discrepancy when the 'leftTime' parameter exceeds 24 hours, causing it to not count down accurately

When the leftTime configuration exceeds 864000, the timer does not start from a value greater than 24 hours. <countdown [config]="{leftTime: `864000`}"></countdown> For example: 1. When leftTime is set to `864000`, the Timer counts down from ...

Utilizing jQuery to attach events to dynamically inserted elements in the DOM

I am having an issue with dynamically adding elements to the DOM and then trying to manipulate their behavior using jQuery's .on() function. However, for some reason, the DOM is not triggering the .on() event for these dynamically added elements. You ...

Error caused by MongoClient TypeError

As a newcomer to NodeJS and someone exploring Dependency Injection for the first time, I encountered an error that led me to seek help. Before asking my question, I reviewed some similar threads on Stack Overflow: [1][2] Upon running my main code, I recei ...

The issue arises when trying to use destructured imports with Mongoose

I've been developing a straightforward Express app with ES6. In the process of creating a schema and model for Mongoose, I encountered an issue with the following syntax: import mongoose, { Schema } from 'mongoose'; const PostSchema = new ...

Adding images sourced from the News API

I have successfully integrated an API from newsapi.org, and it's functioning well. However, the data I receive is only in text format and I would like to include images for each headline. I'm unsure of how to do this since the headlines are dynam ...

Using null or undefined for ternary operator formatting in React applications

When styling a component, what should be used if a specific condition is not fulfilled: null, undefined, or something else? For example: errorStyle: { right: locale === Locales.ARABIC ? 0 : null, left: locale !== Locales.ARABIC ? 0 : null, .. ...

Utilizing Async/Await with Node.js and Mongoose

Learning Promises and async/await programming is a new challenge for me, especially in the context of creating an API using Nodejs, Express, Mongoose, and MongoDB. While most tutorials focus on handling asynchronicity within a single file containing routin ...

Is the scaling of a div with an image affected by odd pixel sizes?

My goal is to create a responsive grid with square images, where the first image is double the size of the others. While I can achieve this using jQuery, I am curious if there's a way to accomplish this purely with CSS. Grid: Here's an example o ...

What is the best way to add randomness to the background colors of mapped elements?

I am looking for a way to randomly change the background color of each element However, when I try to implement it in the code below, the background color ends up being transparent: { modules.map((module, index) => ( <div className='carou ...