I have several DIVs with content that have a data-weight-attribute which is updated regularly via AJAX.
I sort them in the loop where I iterate over the new values coming from the ajax-request.
Because the data-weight can be updated any time to any value, the order can change totally from update to update.
My sorting-logic seems flawed (to say the least ;)) because it only compares every element to its next via .next()
so you have to click "Sort by data-weight" max. 4 times for 4 elements until they are sorted (see fiddle below)
It is important to know that the DIVs to be sorted contain external ressources like images, videos etc so it's important they are moved around and not re-created, because I think when re-inserted into the DOM, the contained ressources get re-loaded which is unacceptable for my use-case.
As it's hard to descripte and maybe understand, here is my fiddle:
http://jsfiddle.net/PdGTK/5/
Update
While the main problem is solved, there is still the problem that when f.e. Youtube-Videos are included, they are re-loaded every time the DIVs are reordered, even if the video doesn't change place in the DOM. That a) looks weird and b) interrupts the video-play. Reading more about the topic, moving iframes in the DOM always seems to make them reload their content - how stupid is that?
Fiddle is updated with a fixed data-weight of 1 for the YT-video so it always stays on top.
http://jsfiddle.net/PdGTK/10/
Ideas very welcome!!
The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.
To display multiple div tags in the same line, we can use the float property in CSS styles. The float property takes left, right,none(default value) and inherit as values. The value left indicates the div tag will be made to float on the left and right to float the div tag to the right.
You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).
Basically what you do is make both divs/columns very tall by adding a padding-bottom: 100% and then "trick the browser" into thinking they aren't that tall using margin-bottom: -100% .
Here's the first way that came to mind:
$("#sortButton").on("click", function () {
var $wrapper = $('#wrapper'),
$articles = $wrapper.find('.article');
[].sort.call($articles, function(a,b) {
return +$(a).attr('data-weight') - +$(b).attr('data-weight');
});
$articles.each(function(){
$wrapper.append(this);
});
});
Seems to work: http://jsfiddle.net/PdGTK/6/
Basically what that is doing is creating a jQuery object ($articles
) that contains all of the articles. Then it sorts the items in the jQuery object according to their data-weight
attribute. Then it goes through them in the new order and appends them to the wrapper - noting that when you .append()
an item that is already in the DOM it gets moved.
This is how you can sort it:
var $wrapper = $('#wrapper'),
$art = $wrapper.children('.article');
$art.sort(function(a, b) {
return +$(a).data('weight') - +$(b).data('weight');
})
.each(function() {
$wrapper.append(this);
});
http://jsfiddle.net/dfsq/PdGTK/8/
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