P粉5712335202023-08-27 10:05:10
您只需使用过渡效果就可以创建动画效果。为了实现这一点,您需要定义容器的宽度和高度,以及底部元素的顶部和左侧位置。
在点击时,您只需交换将变小的元素的类和将变大的元素的类。
这是一个示例的fiddle链接: 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"; }); });