How to swap two Div using animation
P粉191323236
2023-08-26 13:55:33
<p>I have a project where I want a div to appear as a large box and three other divs to appear below as small boxes and when you click on a small box it will change size using a css transition effect and align with the big box Swap positions to smooth movement and size changes. Currently I'm trying to use jQuery but the positioning doesn't work at all. Here's my current example: </p>
<p>https://jsfiddle.net/v3pmhawj/1/</p>
<pre class="brush:php;toolbar:false;">$(function () {
let { left: x1, top: y1 } = $('.full-size-card').offset()
$('.inactive-sheets .card').on('click', function() {
let { left: x2, top: y2 } = $(this).offset()
let curr = $('.full-size-card')
let diffX = x2 - x1
let diffY = y2 - y1
$(this).css({
left: -diffX,
top: -diffY
})
$(this).addClass('full-size-card')
curr.css({
left: diffX,
top: diffY
})
curr.removeClass('full-size-card')
})
})</pre>
<p>If anyone has suggestions involving other libraries or other technologies, I'm willing to listen. I want to be able to move the divs around in the DOM, but as far as I know if you do that you can't do a css transition effect on them because the only way (that I know of) is to remove and re-add a copy of the element in the DOM. </p>
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
CSS
JavaScript