动态地将相撞的元素适应容器大小

5
我有一些绝对定位的元素,它们具有不同的position.topheight,这些元素是从数据库生成的。
我想做的就是通过将它们向右移动并调整宽度以适应<body>容器来避免它们相互碰撞。
但是,我在将'left'位置应用于相撞元素时遇到了问题。
我使用https://sourceforge.net/projects/jquerycollision/来检测碰撞。
以下是最终图片的效果图:

screenshot

$('div').each(function() {
  var name = $(this).text();
  var hits = $(this).collision('div').not(this); // Find colliding elements
  console.log(name + ' collides with: ' + hits.length + ' others');

  if (hits.length > 0) {
    var widthAll = 100 / (hits.length + 1);

    // Shift colliding elements to the right with equal width
    $(hits).add(this).each(function(i) {
      var name = $(this).text();
      $(this).css({ 'left': widthAll * i + '%', 'width': widthAll + '%' });
    });
  }
});
div {
  position: absolute;
  width: 10em;
  font-size: 0.75em;
  color: white;
}

.blue {
  top: 0;
  height: 80%;
  background-color: blue;
}

.red {
  top: 15%;
  height: 5%;
  background-color: red;
}

.yellow {
  top: 17%;
  height: 10%;
  background-color: yellow;
  color: black;
}

.green {
  top: 30%;
  height: 5%;
  background-color: green;
}

.magenta {
  top: 36%;
  height: 3%;
  background-color: magenta;
}

.cyan {
  top: 50%;
  height: 5%;
  background-color: cyan;
  color: black;
}

.brown {
  top: 81%;
  height: 5%;
  background-color: brown;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://rawgit.com/dsbaars/jquery-collision/master/js/jquery-collision.min.js"></script>

<div class='blue'>blue</div>
<div class='red'>red</div>
<div class='yellow'>yellow</div>
<div class='green'>green</div>
<div class='magenta'>magenta</div>
<div class='cyan'>cyan</div>
<div class='brown'>brown</div>

1个回答

1
我认为我已经按照您的要求完成了您的代码。思路如下:
  1. 第一段代码将div向右移动,以避免重叠。
  2. 第二段代码根据页面大小均匀分配div的宽度。
  3. 最后一段代码增加其余div的宽度以占用剩余空间。

"use strict";
var divs = $('div'),
  mx = 0,
  mxs = [0],
  bw = $("body").outerWidth(),
  steps = 1;
divs.each(function(i) {
  for (var j = i + 1; j < divs.length; j++) {
    if (!$(this).data("x")) $(this).data("x", 0);
    if (j < divs.length) {
      var hit = $(this).collision(divs[j]);
      if (hit.length) {
        hit = $(divs[j]);
        hit.css("left", "+=" + Math.ceil($(this).outerWidth()));
        hit.data("x", hit.position().left);
        if (mx < hit.data("x")) {
          mxs.push(mx = hit.data("x"));
          steps++;
        }
      }
    }
  }
});

divs.each(function(i) {
  let iw = $(this).outerWidth(),
    fw = bw / steps;
  $(this).outerWidth(fw);
  for (var j = i + 1; j < divs.length; j++) {
    $(this).collision(divs[j]).css("left", "+=" + Math.ceil((fw - iw) * mxs.indexOf($(divs[j]).data("x"))));
  }
});
divs.each(function() {
  var os = $(this).outerWidth(),
    ts = bw - $(this).position().left;
  $(this).outerWidth(ts);
  if ($(this).collision(divs).not(this).length) {
    $(this).outerWidth(os);
  }
});
body {
  margin: 0;
}
div {
  position: absolute;
  width: 10em;
  font-size: 0.75em;
  color: white;
  left: 0;
}
.blue {
  top: 0;
  height: 80%;
  background-color: blue;
}
.red {
  top: 15%;
  height: 5%;
  background-color: red;
}
.yellow {
  top: 17%;
  height: 10%;
  background-color: yellow;
  color: black;
}
.green {
  top: 20%;
  height: 50%;
  background-color: green;
}
.magenta {
  top: 36%;
  height: 3%;
  background-color: magenta;
}
.cyan {
  top: 50%;
  height: 5%;
  background-color: cyan;
  color: black;
}
.brown {
  top: 81%;
  height: 5%;
  background-color: brown;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/dsbaars/jquery-collision/master/js/jquery-collision.min.js"></script>

<div class='blue'>blue</div>
<div class='red'>red</div>
<div class='yellow'>yellow</div>
<div class='green'>green</div>
<div class='magenta'>magenta</div>
<div class='cyan'>cyan</div>
<div class='brown'>brown</div>

以上代码片段不具有响应式功能。如果您希望它具备响应式功能,只需监听调整大小事件,更改bw的值并重复代码块2和3。

如评论中所述:jquery-collision.min.js存在一些未解决的错误,因此建议使用Alex G提供的https://www.48design.de/de/news/2009/11/20/kollisionsabfrage-per-jquery-plugin-update-v11/作为替代方案。


如果我更改某些元素的“top”和“height”,它仍然重叠。https://jsfiddle.net/n5hLehga/ 截图:https://s28.postimg.org/a14zkpsv1/Untitled.png - Alex G
1
jsfiddle 偶尔会让我惊喜...感谢你们的辛勤工作...代码运行得很好...一个小建议:用 outerWidth 替换 width(这样你就可以随意调整 div 的填充而不会出现问题)。 - Alex G
还有一件事:不知怎么的,你的公式严格依赖于 div 宽度的 10em。如果 10em 有任何不同的值 - 一切就会崩溃。=) - Alex G
你的代码有一个错误:https://jsfiddle.net/56ww0mac/1/embedded/result 青色块与黄色块重叠了。你能看一下吗?谢谢。 - Alex G
我出门旅行了,所以没有办法集中精力找到解决方法。这是一个快速解决问题的方法:https://jsfiddle.net/56ww0mac/2/ - Kiran Shakya
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接