简体   繁体   English

我必须在jquery中阅读childs div的坐标,我想按x坐标进行排序,我的代码如下

[英]I have to read Coordinates of childs div in jquery and i want to sort out by x coordinates my code is below

The current way is providing invalid output. 当前的方法是提供无效的输出。

This is the object i'm trying to sort through 这是我要整理的对象

please check it 请检查一下

   $('.childboxes').each(function () {

    var divs = $(this);
    var divID = $(this).attr('id');
    var position = divs.position();
    var left = position.left;
    var top = position.top;


    AllDivsCoor.push({ "id": divID, "leftcoor": left, "topcoor": top });



});

AllDivsCoor.sort('leftcoor');

You've to write a own sort function like this: 您必须编写一个自己的排序函数,如下所示:

function compareCoords(a, b) {
  if (a.leftcoor < b.leftcoor) return -1;
  if (a.leftcoor > b.leftcoor) return 1;
  return 0;
}
AllDivsCoor.sort(compareCoords);

DEMO: JSFiddle 演示: JSFiddle

Further informations about sort() 有关sort()的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM