$(function() {
// Duration for every swaps
var DURATION = 400;
// The Jquery transition
// Import JQuery UI to get all the transition
var JQUERYUI_EASING = "easeInQuart";
// The index of the li element moved (zero-based)
var START_INDEX = 1;
// The index of destination for the element (zero-based and before the swap)
var DESTINATION_INDEX = 5;
// Call which starts the demo
votingAnimation(START_INDEX, DESTINATION_INDEX);
// ############## The functions ############
// Convenient function to call the recursive one
function votingAnimation(startIndex, destinationIndex) {
// The number of swaps done so far
var numberOfSwapsDone = 0;
var numberOfSwapsToDo = 0;
// Determine the number of swaps to do
if(startIndex < destinationIndex)
numberOfSwapsToDo = destinationIndex - startIndex;
else
numberOfSwapsToDo = startIndex - destinationIndex;
// Let's start
doSwaping(numberOfSwapsDone, numberOfSwapsToDo, startIndex, destinationIndex);
}
// The actual function which gets the job done
function doSwaping(numberOfSwapsDone, numberOfSwapsToDo, startIndex, destinationIndex) {
console.debug(">>>> Do swaping");
// The li elements of the list
// Do it within the function so it's refreshed for every call
var $liElements = $("ul").children();
// Check if we try to push up or down an item
var isPushingDown = startIndex < destinationIndex;
// Index of the top and botto li
var northLiIndex = startIndex + numberOfSwapsDone;
var southLiIndex = startIndex + numberOfSwapsDone + 1;
if(! isPushingDown) { // Pushing up
northLiIndex = startIndex - numberOfSwapsDone - 1;
southLiIndex = startIndex - numberOfSwapsDone;
}
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.