简体   繁体   English

适合任何宽度/高度容器的正方形网格

[英]Grid of squares that fits within any width/height container

I am trying to create a grid of squares that could fit in any dimensions container (ie. the squares resize themselves or add/delete new ones if the container width/height are changed).我正在尝试创建一个可以适合任何尺寸容器的正方形网格(即,如果容器宽度/高度发生变化,正方形会自行调整大小或添加/删除新的)。

I am going the Javascript route for now (and Jquery, for now) - there might be a flexgrid solution but since I plan to populate my squares with some kind of cellular automata type thingie, I figured that it wouldn't hurt.我现在要走 Javascript 路线(和 Jquery,现在) - 可能有一个 flexgrid 解决方案,但由于我计划用某种细胞自动机类型的东西填充我的正方形,我认为它不会受到伤害。 This doesn't solve my problem, since the number of lines of squares seems to be fixed. 并不能解决我的问题,因为正方形的行数似乎是固定的。

Here is what I have so far:这是我到目前为止所拥有的:

var screen = {
  width: 0, // these I get with jquery, on load and on resize events
  height: 0
}

var values = {
  min: 100, // minimum square size
  max: 500 // maximum square size
}

var findSize = function() {
  var r = 1; 
  var currVal = values.min;
  while (r > 0) {
    if((screen.width % currVal) === (screen.height % currVal)) {
      // this should mean that they are both divisible by this value, right?
      // get out of the loop and return value
      r = 0;
      return currVal;
    } else if (currVal > values.max ) {
      // if we exceed the maximum size, get out of the loop and return 0
      r = 0;
      return 0;
    } else {
      // if not, increment a bit
      currVal += 0.25; // increment the value to check the modulo against
    }
  }
}

Calling the findSize() function should return either the dimensions of the square (from which I can then build my grid easily, with either float ed squares or absolutely positioned ones.调用findSize() function 应该返回正方形的尺寸(然后我可以很容易地从中构建我的网格,使用浮动的正方形或绝对定位的正方形。

The problem is that it doesn't.问题是它没有。 Well it sometimes does.好吧,有时确实如此。 And it also pretty often gives me nothing...而且它也经常给我什么...

不工作的网格

The border are done with box-shadow so it shouldn't affect the dimensions.边框是用box-shadow完成的,所以它不应该影响尺寸。

So I am wondering...所以我想知道...

  • Where is my code faulty?我的代码哪里出错了?
  • Can I change my function so it return always something (maybe with smaller incrementations?).我可以改变我的 function 让它总是返回一些东西(也许增量更小?)。 I can work with rounded values for display purpose.我可以使用四舍五入的值进行显示。
    • The brute force aspect doesn't seem too efficient.蛮力方面似乎不太有效。 Is there a way to refactor this so the loop is shorter?有没有办法重构这个,所以循环更短?

Thanks a lot!非常感谢!

The problem with this code is it tries to find the solution but fails to account for answers between each deltas (0.25).这段代码的问题是它试图找到解决方案,但无法解释每个增量之间的答案(0.25)。

Also, if there can be any numbers of cells (added removed automatically) then the answer can also be "always 100".此外,如果可以有任意数量的单元格(自动添加删除),那么答案也可以是“总是 100”。

I guess what you're trying to achieve here is a "best fit" that could leave no borders horizontally and vertically at the same time.我想你在这里想要实现的是一个“最合适的”,它可以同时在水平和垂直方向上不留边界。 I'm not sure if there is a proper answer to that question (you probably need to search for Greatest common divisor , close to what you are doing), and I wonder if something like the code below wouldn't work in your case:我不确定该问题是否有正确的答案(您可能需要搜索Greatest common divisor ,接近您正在做的事情),我想知道下面的代码是否不适用于您的情况:

var findSize = function() {
  var ratio = screen.width / screen.height; // get the ratio between width and height
  if (ratio > 1) {
    ratio = 1 / ratio; // always have it always <= 1
  }
  var size = values.max * ratio; // size between 0 and max
  if (size < values.min) {
    return values.min; // failed, could try other things here
  }
  return size; // size between min and max
}

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

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