P粉5712335202023-08-27 10:05:10
You can create animation effects simply by using transition effects. To achieve this, you need to define the width and height of the container, as well as the top and left position of the bottom element.
On click, you simply swap the class of the element that will become smaller and the class of the element that will become larger.
This is a fiddle link with an example: https://jsfiddle.net/fkd3ybwx/210/
HTML
<div class="card-container"> <div class="card large">A</div> <div class="card small">B</div> <div class="card small">C</div> <div class="card small">D</div> </div>
CSS
.card-container { position: relative; } .card { transition: all ease 1s; position: absolute; font-size: 24px; border: white 4px solid; box-sizing: border-box; cursor: pointer; } .small { width: 100px; height: 100px; background: blue; left: 0; top: 300px; } .small ~ .small { left: 100px; background: green; } .small ~ .small ~ .small { left: 200px; background: yellow; } .large { width: 300px; height: 300px; background: red; left: 0px; top: 0px; }
JavaScript
const smallCards = document.querySelectorAll('.card'); smallCards.forEach((smallCard) => { smallCard.addEventListener('click', (event) => { const largeCard = document.querySelector('.large'); largeCard.className = "card small"; event.target.className = "card large"; }); });