I am looking to implement a rotating three-card display on click, and have come up with the following code:
$('.box1').click(function(){
$('.box1').toggleClass('removeanimate');
$(this).toggleClass('go');
$('.box2').removeClass('go');
});
$('.box2').click(function(){
$(this).toggleClass('go');
$('.box3').removeClass('go')
});
$('.box3').click(function(){
$(this).toggleClass('go');
$('.box1').toggleClass('go');
$('.box2').toggleClass('go');
});
.wrapper{
position:static;
}
.box{
width:200px;
height:100px;
position:absolute;
margin:5px;
cursor:pointer;
overflow:hidden;
text-align: center;
line-height: 100px;
}
.box1 {
z-index:2;
}
.box2{
z-index:1;
}
.box3{
z-index:0;
}
.go{
height:0;
width:0;
}
.box:nth-child(1) {
background: green;
}
.box:nth-child(2) {
background: red;
}
.box:nth-child(3) {
background: orange;
}
.removeanimate: {
transform: scale(0.7);
transform-origin: left;
background-color: pink;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
<div class="box box1"><h2>I'm block 1</h2></div>
<div class="box box2"><h2>I'm block 2</h2></div>
<div class="box box3"><h2>I'm block 3</h2></div>
</div>
Now, I have the following questions regarding this implementation:
1- It seems that the animation between cards using .removeanimate
is not working properly. How can this be resolved?
2- I am concerned about scalability in the script part. Adding more boxes manually for the onclick function is not ideal. Is there a better way to handle this without disrupting the rotation behavior?