You can do it this way:
First change the position
of div.frente
of absolute
for fixed
to avoid the horizontal scrollbar when moving it out of the window, and add the property transition
that will make the animation in CSS:
.frente{
position: fixed; /* POSITION ALTERADO */
top: 0;
right: 0;
display: flex;
height: 100vh;
width: 100vw;
justify-content: center;
align-items: center;
background: #aaa;
transition: right .5s ease-in; /* ANIMAÇÃO */
}
Now create a class for the div where the right
will be -100vw
, causing the div to move out of the screen on the right:
.frente.sai{
right: -100vw;
}
When the div.frente
win the class .sai
, will fire the transition
, where you can adjust the time and type of transition: put .5s
, which are 500 milliseconds or half a second, and ease-in
(a list of types of transitions you can see in this document).
Put a class in tags <a>
"open div" and "close div" which will greatly facilitate the JS code and will prevent other links you may have on the page from activating the animation. Put the same class in both tags. In the example below I put the class .controle
:
<div class="atras">
<a class="controle" href="#">abrir div </a>
</div>
<div class="frente">
<a class="controle" href="#">fechar div</a>
</div>
Done that, just make one toggle class .sai
in div.frente
. A toggle
basically is to insert/remove the class of the element each time it is called.
In jQuery would look like this:
$(".controle").on("click", function(e){
e.preventDefault();
$(".frente").toggleClass("sai");
});
In pure JS like this:
const controles = document.querySelectorAll(".controle");
for(let el of controles){
el.onclick = function(e){
e.preventDefault();
document.querySelector(".frente").classList.toggle("sai");
}
}
The e.preventDefault();
is to prevent the href="#"
add the hash
#
at the bottom of the page URL.
I’ll put an example below using jQuery, but you can switch to the pure JS version if you want:
$(".controle").on("click", function(e){
e.preventDefault();
$(".frente").toggleClass("sai");
});
.atras{
display: flex;
flex: 1;
height: 100vh;
justify-content: center;
align-items: center;
}
.frente{
position: fixed;
top: 0;
right: 0;
display: flex;
height: 100vh;
width: 100vw;
justify-content: center;
align-items: center;
background: #aaa;
transition: right .5s ease-in;
}
.frente.sai{
right: -100vw;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="atras">
<a class="controle" href="#">abrir div </a>
</div>
<div class="frente">
<a class="controle" href="#">fechar div</a>
</div>
worked perfectly for what I needed, thank you very much Ugo! but tell me what it’s for
scaleX(0)
and thattransform-origin
?– leonardo vita
@leonardovita scaleX eh to put the width of the element with 0 horizontal scale, type width 0, and the Transform-Origen and to determine by where I want the transfor happens, type right to left or the other way. If the answer solved the problem remember to mark one of them with the below arrows of the answer you choose
– hugocsl