Is it possible in jQuery to swap the position of two containers - both visually and and in the DOM, but also animate the result?
I have:
<div id="container">
<div id="one"><a class="moveUp">Up</a></div>
<div id="two"><a class="moveUp">Up</a></div>
<div id="three"><a class="moveUp">Up</a></div>
</div>
Clicking the link will swap that div's position with the one above. Most examples use absolute positioning and offsets the top property to achieve this... but I need to read the data in the order it is displayed on screen when the user calls a function somewhere else.
So I came up with this:
$('#container div').on('click', '.moveUp', function() {
divInQuestion = $(this).closest('div').attr('id'); //id of this div
if (divInQuestion > '1') {
switchWithDiv = divInQuestion - 1; //id of other div
firstSelector = $('#chapterset-'+divInQuestion).html(); //save contents of each div. I actually don't
secondSelector = $('#chapterset-'+switchWithDiv).html(); //do it this way, I regenerate the content
$('#chapterset-'+divInQuestion).html()firstSelector.replaceWith(secondSelector); //replace div with other div
$('#chapterset-'+switchWithDiv).html()secondSelector.replaceWith(firstSelector);
}
});
Now, my code is actually more complex than this, but it gives the shell of what I'm doing.
jQuery works fine but, how would I include an animation?
PS: Tried to get a jsFiddle going but their servers might be down atm??!!?
The following is a module with functions that demonstrates a simple way how to swap two HTML element locations in the Dom tree using JavaScript. This method retains any existing event listeners. 1. Swap Element Locations The example below demonstrates how to swap two HTML element locations. 1. Swap Element Locations <!-- 2. Utils Namespace
CSS allows us to animate HTML elements without making use of JavaScript. To use CSS animation, you must first specify some keyframes for the animation. Keyframes hold the styles that the element will have at certain times. For proper understanding, I will be explaining the basic properties we will be using.
In this article, we will discuss a way in which one can swap two array elements in JavaScript in a single line. The input and output would be as follows. This swapping can be done by writing the 2 array elements we want to reverse in order and in square brackets on the left-hand side.
The container element should be created with style = "position: relative". The animation element should be created with style = "position: absolute". JavaScript animations are done by programming gradual changes in an element's style. The changes are called by a timer.
Try animating it first, to make the visual swap, and then swap them in the DOM after:
http://jsfiddle.net/BYossarian/GUsYQ/5/
HTML:
<div id="container">
<div id="one"><a class="moveUp">Up 1</a></div>
<div id="two"><a class="moveUp">Up 2</a></div>
<div id="three"><a class="moveUp">Up 3</a></div>
</div>
CSS:
#container {
width: 200px;
border: 1px solid black;
position: relative;
}
#container div {
border: 1px solid black;
position: relative;
}
#container a {
display: block;
}
JS:
var animating = false;
$('#container').on('click', '.moveUp', function () {
if (animating) {return;}
var clickedDiv = $(this).closest('div'),
prevDiv = clickedDiv.prev(),
distance = clickedDiv.outerHeight();
if (prevDiv.length) {
animating = true;
$.when(clickedDiv.animate({
top: -distance
}, 600),
prevDiv.animate({
top: distance
}, 600)).done(function () {
prevDiv.css('top', '0px');
clickedDiv.css('top', '0px');
clickedDiv.insertBefore(prevDiv);
animating = false;
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With