【发布时间】:2019-11-06 19:59:33
【问题描述】:
抱歉,我无法显示代码,我只是不知道该怎么做。
我想用分数实现类似display: flex/grid 的效果:
给定容器宽度:200px
还有物品:[0.5, 1, 50px]
我应该能够计算相对于彼此的小数:
item1: 50 (half the size of item 2)
item2: 100 (double the size of item 1)
item3: 50 (static size not a fraction)
const totalSize = 900;
const items = [1, 0.2, 75, 0.6, 1, 100];
const totalStaticSize = items.filter(n => n > 1).reduce((acc, n) => acc + n, 0);
const derivedItems = items.map(item => {
if (item > 1) {
// A static value.
return item;
}
// How to calculate this fractional item relative to others?
return item;
});
console.log(derivedItems);
【问题讨论】:
-
这些项目不是有效的 javascript。值是否编码为字符串?
-
只是一个例子,我知道哪些是非分数的,但为此我们假设任何超过 1 的都是。添加了一个可运行的 sn-p。
-
item1 = (item1 / (item1 + item2)) * space available?
标签: javascript math