【问题标题】:Organize container content by container height按容器高度组织容器内容
【发布时间】:2013-06-19 05:55:18
【问题描述】:

我的网站有 2 个容器,每个容器都有一堆相关的内容。将哪些内容放入哪个容器中的组织很重要,但不如容器高度尽可能接近那么重要。

代码:

<button>Click to add more boxes</button>
<div id="wrapper" align="center">
    <div id="container1">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
    </div>
    <div id="container2">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
    </div>
</div>

Jsfiddle


问题:

添加更多项目后您会看到,只是有时一个容器的内容会比另一个容器多得多,但确实会发生。

当这种情况发生时,我想把剩下的物品放在较大的容器中,然后以一种使每个容器的高度尽可能接近的方式将它们分配到容器中。

我该怎么做呢?

【问题讨论】:

  • +1 有趣的问题。

标签: javascript jquery


【解决方案1】:

我已经发布了使用的理想算法。仍然可以进行一些粗略的计算以使容器看起来整洁。

Try this bin

JS 代码

var $container1 = $('#container1');
var $container2 = $('#container2');
$('.box').each(function(){
    $(this).height(100 * Math.random());
});
var move = $container1.outerWidth();
$container2.css("margin-left",move + 10);

// Let us try to minimize the height difference
var h1 = $container1.height();
var h2 = $container2.height();
var diff = 20;

var currentDiff = h1-h2;
if (currentDiff > diff) {
  alert('1 is bigger than 2');
  reArrange($container1, $container2, Math.abs(currentDiff), diff);
}
if (currentDiff < -diff) {
   alert('2nd is bigger than 1st');
  reArrange($container2, $container1, Math.abs(currentDiff), diff);
}

// Logic: Send the minium height box to smaller container
function reArrange(largeC, smallC, currentDiff, diff) {
    var minHeightBox = null;
    var minH = 10000000000;
  
    $('.box', largeC).each(function(){
      if ($(this).height() < minH) 
        minHeightBox = this;
        minH = $(this).height();
    });
  
    // If transfering this to other reduces the difference, transfer this otherwise returh false
  if (minHeightBox !== null) {
    var newDiff = (largeC.height() - minH) - (smallC.height() + minH);
    newDiff = Math.abs(newDiff);
    console.log(newDiff);
    if (newDiff < currentDiff) {
      smallC.append(minHeightBox);
      currentDiff = largeC.height() - smallC.height();      
      //Recursively call the method
      if (currentDiff > diff) 
        reArrange(largeC, smallC, currentDiff, diff);
    }
  }
}

【讨论】:

  • 总比没有好,但它肯定不会把事情搞砸。您说它正在开发中...另外,我可以解释一下您如何仅在超出限制的元素上运行它,因为我找不到那在哪里
  • @RyanSaxe 您可以通过调整代码中 diff 的值来微调差异。看看这个jsbin.com/upacip/3/edit 当然它不会让事情变得完全重要,因为我使用了贪婪的方法,并且我已经提到了一个标准方法,如果你想要最好的,你应该遵循它。
【解决方案2】:

有一个标准算法Subset Sum Problem 可以解决这个问题。

问题说

You have n integers. Divide n integers into two sets such that |Set1| - |Set2| is minimum. where |Set(i)| represents the sum of elements in ith set.

我看到你在这里遇到了类似的问题。假设您的容器为两组,每个盒子的高度为将放入该组中的整数。问题是我们必须以这样一种方式划分整数(盒子),以使两个容器的高度保持最小。

相关链接:

Subset Sum Problem - Wiki

Geeks for Geeks tutorial - 好人

【讨论】:

  • 这不是我想做的。我不想重新安排整个事情以最小化高度......这将更容易让我的头脑和编码......我想检测一个明显更大的时间,然后将额外的元素放入较大的容器并以容器尽可能靠近的方式拆分它们……这有意义吗?
  • @RyanSaxe 是的,我同意你的问题,但是必须决定需要移动哪些盒子来平衡高度差,并且应该使用这个算法来找出应该移动哪些盒子。我在下一个答案中提出了一个基于贪婪的(简单的解决方案),这似乎是好的,但不是最好的。
  • 我想你不明白...唯一允许移动的盒子是那些使较大的容器变得太大的盒子
  • @RyanSaxe 是的,我也做过同样的事情。确保您阅读并理解代码。我已经移动了使容器变大的盒子。查看其他解决方案,我认为这就是您想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
相关资源
最近更新 更多