Cleverly use z-index and subsequent sibling selectors to achieve the effect of circular flipping
See the effect first
Click on the card to switch one, and the entire implementation process does not use js. In addition to the two cards, this sample actually has two transparent inputs. You actually click on these two inputs, and these two inputs will modify their own z-index properties after clicking, ensuring that you will definitely click on another one next time you click.
z-index
After we use positioning (posation:absolute,fixed,relative) on elements, the elements may overlap. In addition to the principle of catching up in the html structure, we can also use z-index to control the covering relationship of elements. The larger the z-index value, the more the element is at the top.
Here is a short example:
Let’s try clicking on these two giant radio buttons
The element assigned z-index:2 will cover the element of z-index:1. If we completely overlap these two radio buttons and assign a lower z-index to the selected radio button, we can ensure that the one that is clicked each time is the unselected one. This is the core logic of this flip case.
Let’s get started
The following is the full code for the flip card example:
<divclass="box"><!-- The two inputs need to be placed before the card but cover it. We use z-index to achieve this --><inputclass="radio1"type="radio"name="card"/><inputclass="radio2"type="radio"name="card"/><!-- Two cards --><divstyle="background:#F1948A"class="card1"></div><divstyle="background:#AED6F1"class="card2"></div></div><style>[class*="card"]{//Settheinitialstyleforthecardwidth:200px;height:300px;position:absolute;border-radius:16px;box-shadow:0010pxrgba(0,0,0,0.2);transition:all1s;}.box{height:300px;width:200px;position:relative;}[class*="radio"]{//Thewidthandheightofthetworadiobuttonsareconsistentwiththecard,andtheyarecompletelytransparent,andthencoveredonthecardposition:absolute;width:200px;height:300px;z-index:2;margin:0;opacity:0;}.radio1:checked~.card1{animation:card.6s;top:0;scale:.98;transition-duration:.6s}.radio1:checked~.card2{z-index:1;top:10px;transition-duration:.6s}.radio1:checked{z-index:0;}//Theselectedradiobuttonsetsz-indexto0//Afterbeingclicked,itwillautomaticallymovetothebottom,sothatthenextclickwillhitanotherradiobutton.radio2:checked~.card2{animation:card.6s;top:0;scale:.98;transition-duration:.6s}//Selectthecardthroughthesubsequentsiblingselector,sothatselectingadifferentradiobuttonwillcontrolthecardtoflipone..radio2:checked~.card1{z-index:1;top:10px;transition-duration:.6s}.radio2:checked{z-index:0;}//Sameasanotherradiobutton@keyframescard{//Defineaflipanimationhere0%{left:0;z-index:2;top:10px}50%{left:230px;z-index:2;rotate:10deg;}51%{left:230px;z-index:0;rotate:10deg;}100%{left:0;z-index:0;scale:.98;}}</style>
But it seems that this can only flip between two cards, so how can we increase the number of cards?
At this time, you only need to combine the ideas in Realizing the Rating Component without Using JS. In this case, selecting a star can select all the stars before this star. Similarly, we can set the z-index value of all the previous radio buttons to 0 when selecting a radio button, and reset all z-index after selecting the last radio button to achieve a cyclic flip.
Of course, the theoretical significance of this case is greater than the practical significance. Every time a card is added, a lot of css code will be generated, which is not as good as js. But this case can still be used as a good case to illustrate the use of z-index.