javascript - 动态适应容器中的碰撞元素

标签 javascript jquery css jquery-collision

我用不同的 position.top 绝对定位元素和 height从数据库生成。

我想做的就是通过将这些元素向右移动,同时调整宽度以适应 <body> 来消除这些元素的碰撞。容器。

我在将“左”位置应用于碰撞元素时遇到问题。

我使用 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. 第一段代码将 div 向右移动,这样它们就不会重叠。
  2. 第二个 block 使 div 的宽度根据正文的大小均匀分布。
  3. 最后一个 block 增加其余 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 有一些 Unresolved 错误,因此,正如 Alex G 所建议的那样,https://www.48design.de/de/news/2009/11/20/kollisionsabfrage-per-jquery-plugin-update-v11/可能是另一种选择。

关于javascript - 动态适应容器中的碰撞元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41885588/

相关文章:

javascript - 在 ng-click AngularJS 上附加一个带有字符串的文本区域输入类型

javascript - div 固定在滚动的某个点

javascript - 在 React 的组件构造函数中传递函数

Javascript的按位运算符优化?

javascript - 堆叠条形图比例不一致问题

html - Bootstrap 侧导航栏问题

html - 切换按钮和搜索栏偶尔显示错位

javascript - 取消父项的监听器是否适用于 Firebase 中的子项

jquery - 计算列表元素的数量并创建 "more"下拉列表

javascript - jQuery on ul 上的函数和其中的 ul