What should I do if I want to rearrange the position on the source based on the coordinates of the position in jquery?
Example: Sort left values in descending order
<div style="position:relative;left:10px">1</div>
<div style="position:relative;left:0px">2</div>
<div style="position:relative;left:15px">3</div>
<div style="position:relative;left:5px">4</div>
↓
<div style="position:relative;left:0px">2</div>
<div style="position:relative;left:5px">4</div>
<div style="position:relative;left:10px">1</div>
<div style="position:relative;left:15px">3</div>
var target=$('div');// Specify as all divs are covered
var sorted_obj = target.sort(function(a,b){
return parseInt($(a).css('left'))-parseInt($(b).css('left'));
}));
target.parent().append(sorted_obj);
© 2024 OneMinuteCode. All rights reserved.