Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
NOW THE PROBLEM:

I designed the code such that:

when I click on the body (or document) of the second page, the first page moves to the left (-100vw) and then sits on the second page (0vw, z-index:1)
when I click on the body (or document) of the first page, the second page moves to the right (100vw) and then sits on the first page (0vw, z-index:1)

(I used keyframes in CSS).

When you view the effect in your browser you'll notice that at some point while animating from one page to the other, there will be a "jumpy" effect.
At a point, the animation flows smoothly, then, at another point, it gives this "jumpy" effect
(and that's really annoying)

Please, someone, help me fix this code by removing the "jumpy" effect to give the desired transition.
Below are the HTML,CSS,JS files.

HTML
<body>
    <section id="pg_one">
        <span>
            <label for="txt">Enter name here: </label>
            <input type="text" id="txt"/>
        </span>
    </section>
    <section id="pg_two">
        <span>
            <label for="pwd">Choose password: </label>
            <input type="password" name="" id="pwd"/>
        </span>
    </section>
</body>

CSS
*,
*::after,
*::before{
    margin: 0;
    padding: 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

body{
    width: 100vw;
    height: 100vh;
    overflow: hidden;
}

section#pg_one{
    display: grid;
    align-items: center;
    justify-items: center;
    width: 100%;
    height: 100%;
    position: absolute;
    background-color: #5944f0;
    /* z-index: 1; */
}

.pg_one{
    animation-name: p1off;
    -webkit-animation-duration: 1000ms;
    animation-duration: 1000ms;
    -webkit-animation-timing-function: linear;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
}

@keyframes p1off {
    0%{
        -webkit-transform: translateX(0);
        -ms-transform: translateX(0);
        transform: translateX(0);
    }
    50%{
        -webkit-transform: translateX(-100vw);
        -ms-transform: translateX(-100vw);
        transform: translateX(-100vw);
    }
    100%{
        -webkit-transform: translateX(0);
        -ms-transform: translateX(0);
        transform: translateX(0);
        z-index: 1;
    }
}

section#pg_two{
    position: absolute;
    width: 100%;
    height: 100%;
    display: grid;
    align-items: center;
    justify-items: center;
    background-color: #d0277f;
}

.pg_two{
    animation-name: p2off;
    -webkit-animation-duration: 1000ms;
    animation-duration: 1000ms;
    -webkit-animation-timing-function: linear;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
}

@keyframes p2off {
    0%{
        -webkit-transform: translateX(0);
        -ms-transform: translateX(0);
        transform: translateX(0);
    }
    50%{
        -webkit-transform: translateX(100vw);
        -ms-transform: translateX(100vw);
        transform: translateX(100vw);
    }
    100%{
        -webkit-transform: translateX(0);
        -ms-transform: translateX(0);
        transform: translateX(0);
        z-index: 1;
    }
}

JavaScript
var pg_one = document.getElementById("pg_one");
var pg_two = document.getElementById("pg_two");

pg_one.onclick=()=>{
    pg_two.className = "pg_two";
    pg_one.className = "";
}
pg_two.onclick=()=>{
    pg_one.className = "pg_one";
    pg_two.className = "";
}


What I have tried:

I have tried manipulating the z-index in the CSS keyframes.
Posted
Updated 26-Oct-21 23:50pm

1 solution

You need to change how your animation works:
CSS
section {
    display: grid;
    align-items: center;
    justify-items: center;
    width: 100vw;
    height: 100vh;
    position: absolute;
    -webkit-animation-duration: 1000ms;
    animation-duration: 1000ms;
    -webkit-animation-timing-function: linear;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
}

#pg_one {
    --hidden-left: -100vw;
    background-color: #5944f0;
    -webkit-transform: translateX(0);
    -ms-transform: translateX(0);
    transform: translateX(0);
}

#pg_two {
    --hidden-left: 100vw;
    background-color: #d0277f;
    -webkit-transform: translateX(var(--hidden-left));
    -ms-transform: translateX(var(--hidden-left));
    transform: translateX(var(--hidden-left));
}

@keyframes hide {
    0% {
        -webkit-transform: translateX(0);
        -ms-transform: translateX(0);
        transform: translateX(0);
    }
    100% {
        -webkit-transform: translateX(var(--hidden-left));
        -ms-transform: translateX(var(--hidden-left));
        transform: translateX(var(--hidden-left));
    }
}

@keyframes show {
    0% {
        -webkit-transform: translateX(var(--hidden-left));
        -ms-transform: translateX(var(--hidden-left));
        transform: translateX(var(--hidden-left));
    }
    100% {
        -webkit-transform: translateX(0);
        -ms-transform: translateX(0);
        transform: translateX(0);
    }
}

.hide {
    animation-name: hide;
}

.show {
    animation-name: show;
}
JavaScript
const pg_one = document.getElementById("pg_one");
const pg_two = document.getElementById("pg_two");

pg_one.addEventListener("click", () => {
    pg_one.classList.remove("show");
    pg_one.classList.add("hide");
    pg_two.classList.remove("hide");
    pg_two.classList.add("show");
});

pg_two.addEventListener("click", () => {
    pg_two.classList.remove("show");
    pg_two.classList.add("hide");
    pg_one.classList.remove("hide");
    pg_one.classList.add("show");
});
Demo[^]
 
Share this answer
 
Comments
[no name] 27-Oct-21 13:52pm    
First of all, thank you Richard Deeming for your answer.
I actually wanted it to be just as the animation in the initial stage works.
Hope you understand
[no name] 27-Oct-21 16:10pm    
The answer from Richard Deeming is NOT what I'm looking for...
Richard Deeming 28-Oct-21 3:41am    
Firstly, have a little patience. Your first reply was posted at 7PM in my time-zone.

Secondly, you're going to need to explain precisely what you want. Your question says you want to "fix" the animation, but you've rejected the "fixed" version saying you want it to work exactly like the version in your question. But if the version in your question is exactly how you want it to work, then there's nothing to "fix"!
[no name] 28-Oct-21 5:18am    
Notice I said: "I actually wanted it to be just as the animation in the initial stage works"

Thank you very much for helping me out with this bug (and for replying, that meant a lot!)

Actually, what I meant by fixing the animation was this:
The page "on which you click" should cause the other page to slide in to sit on top.
Click on red, blue slides in (from the left) to sit on top;
Click on blue, red slides in (from the right) to sit on top.

The page "on which you click" SHOULD NOT MOVE when the other guy comes in to SIT RIGHT ON TOP.
Get the idea? (Forgive me for not clarifying this a lot earlier.)
Richard Deeming 28-Oct-21 5:23am    
So something like this?
Demo[^]

Changes:
@keyframes hide {
    0% {
        ...
        z-index: 0;
    }
    99% {
        -webkit-transform: translateX(0);
        -ms-transform: translateX(0);
        transform: translateX(0);
        z-index: 0;
    }
    100% {
        ...
        z-index: 0;
    }
}

@keyframes show {
    0% {
        ...
        z-index: 1;
    }
    100% {
        ...
        z-index: 1;
    }
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900