Here is my snippet :
.item {
display:inline-block;
width:120px;
height:120px;
border:1px solid red;
}
<div id="listing">
<div data-value="5" class="item">item 5</div>
<div data-value="3" class="item">item 3</div>
<div data-value="2" class="item">item 2</div>
<div data-value="1" class="item">item 1</div>
<div data-value="4" class="item">item 4</div>
</div>
Here is my snippet :
.item {
display:inline-block;
width:120px;
height:120px;
border:1px solid red;
}
<div id="listing">
<div data-value="5" class="item">item 5</div>
<div data-value="3" class="item">item 3</div>
<div data-value="2" class="item">item 2</div>
<div data-value="1" class="item">item 1</div>
<div data-value="4" class="item">item 4</div>
</div>
I would like to sort the div
s based on the value of the data-value
attribute, but I want to show the items being rearranged using an animation. How can I do this?
- possible duplicate of Sort Divs in Jquery Based on Attribute 'data-sort'? – Hexaquark Commented Jul 10, 2015 at 21:27
2 Answers
Reset to default 7Here's how you can animate the sort:
- Loop through all the elements and store their positions
- Position the elements absolutely at their original positions
- Get the position of the item that will be replaced, and animate to that position.
- (Optional) After the animation is plete, you may want to swap out the absolutely positioned items with regular items in sorted order, so the DOM order of the elements is correct.
function animateSort(parent, child, sortAttribute) {
var promises = [];
var positions = [];
var originals = $(parent).find(child);
var sorted = originals.toArray().sort(function(a, b) {
// return negative for smaller, zero for equal and positive for greater
return $(a).attr(sortAttribute) - $(b).attr(sortAttribute);
});
originals.each(function() {
//store original positions
positions.push($(this).position());
}).each(function(originalIndex) {
//change items to absolute position
var $this = $(this);
var newIndex = sorted.indexOf(this);
sorted[newIndex] = $this.clone(); //copy the original item before messing with its positioning
$this.css("position", "absolute").css("top", positions[originalIndex].top + "px").css("left", positions[originalIndex].left + "px");
//animate to the new position
var promise = $this.animate({
top: positions[newIndex].top + "px",
left: positions[newIndex].left + "px"
}, 1000);
promises.push(promise);
});
//instead of leaving the items out-of-order and positioned, replace them in sorted order
$.when.apply($, promises).done(function() {
originals.each(function(index) {
$(this).replaceWith(sorted[index]);
});
});
}
$(function() {
$("input").click(function() {
animateSort("#listing", "div", "data-value");
});
});
.item {
display: inline-block;
width: 120px;
height: 120px;
border: 1px solid red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" value="Sort" />
<div id="listing">
<div data-value="5" class="item">item 5</div>
<div data-value="3" class="item">item 3</div>
<div data-value="2" class="item">item 2</div>
<div data-value="1" class="item">item 1</div>
<div data-value="4" class="item">item 4</div>
</div>
Though you could easily do this without jquery, here is a (non-animated) way to sort them:
var sorted = $('#listing div').sort(function(a,b) {
return $(a).data('value') - $(b).data('value');
});
$('#listing').html(sorted);
https://jsfiddle/yne89aw4/
I'll see what I can do about animating the sorting